wskaznik do elementu klasy wewnątrz klasy
Ostatnio zmodyfikowano 2015-07-03 17:33
darko202 |
» 2015-07-03 12:33:37 |
|
notabigthreat |
» 2015-07-03 13:26:55 polimorfizm właśnie pozwala przechowywać obiekty różnych klas w jednym kontenerze |
Nie w std::vector. |
|
RazzorFlame |
» 2015-07-03 13:44:33 @Up, skąd te założenie? Prawda jest taka, że ani darko202 i notabigthreat nie mają pełnej racji. class Base { public: Base( int xx ) { x = xx; } int x; }; class Derived : public Base { public: Derived( int xx, yy ) : Base( xx ) { y = yy; } int y; };
std::vector < Base > VecBase; VecBase.push_back( Derived( 0, 1 ) ); std::vector < Base *> VecBasePtr; VecBasePtr.push_back( new Derived( 1, 2 ) );
------------------------------------------------------ Teraz odnośnie ostatniego postu założyciela tematu. Przedstawię Ci taką podstawową hierarchię: class Object { public: bool CanPlaceInWorld; std::wstring ObjectIdentifier; Object( std::wstring ArgObjectIdentifier ) : ObjectIdentifier( ArgObjectIdentifier ) { CanPlaceInWorld = false; } }; class Asset : public Object { typedef Object Super; public: Asset( std::wstring ArgObjectIdentifier ) : Super( ArgObjectIdentifier ) { CanPlaceInWorld = false; } virtual void DrawWithTransform( Transform transform ) { } }; class Mesh : public Asset { typedef Asset Super; public: Mesh( std::wstring ArgObjectIdentifier ) : Super( ArgObjectIdentifier ) { CanPlaceInWorld = false; } virtual void DrawWithTransform( Transform transform ) override { } };
class SceneActor;
class BaseComponent : public Object { typedef Object Super; protected: Asset * UsedAsset; Transform AssetTransform; SceneActor * Parent; public: BaseComponent( std::wstring ArgObjectIdentifier ) : Super( ArgObjectIdentifier ) { CanPlaceInWorld = false; } virtual void OnDraw() { if( Parent ) { Transform WorldTransform = AssetTransform + Parent->ActorTransform; UsedAsset->DrawWithTransform( WorldTransform ); } } }; class MeshComponent : public BaseComponent { typedef BaseComponent Super; public: MeshComponent( std::wstring ArgObjectIdentifier, SceneActor * ArgParent, Mesh * ArgUsedAssetMesh ) : Super( ArgObjectIdentifier ) , Parent( ArgParent ) , UsedAsset( ArgUsedAsset ) { CanPlaceInWorld = false; } }; class SceneActor : public Object { typedef Object Super; protected: std::vector < BaseComponent *> Components; Transform ActorTransform; void AddNewComponent( BaseComponent * ArgNewComponent ) { Components.push_back( ArgNewComponent ); } public: SceneActor( std::wstring ArgObjectIdentifier ) : Super( ArgObjectIdentifier ) { CanPlaceInWorld = true; } void OnDraw() { for( int i = 0; i < Components.size(); i++ ) Components[ i ]->OnDraw(); } };
class Player : public SceneActor { typedef SceneActor Super; protected: public: Player( std::wstring ArgObjectIdentifier ) : Super( ArgObjectIdentifier ) { CanPlaceInWorld = true; Mesh * MeshAsset = Engine::FindAsset( std::wstring( L"Mesh.obj" ) ); MeshComponent * DefaultMesh = new MeshComponent( std::wstring( L"DefaultMesh" ), this, MeshAsset ); AddNewComponent( DefaultMesh ); } }
class Engine { static std::vector < Object *> Objects; public: static Asset FindAsset( std::wstring ArgObjectIdentifier ) { } };
Ale się rozpisałem... Powinienem jakiś artykuł napisać. Jak czegoś nie zrozumiałeś to pytaj ;] |
|
qrchack Temat założony przez niniejszego użytkownika |
» 2015-07-03 17:33:30 @darko202 @notabigthreat Absolutnie nie zapomniałem o polimorfizmie :]
I dlatego mam klase cObiekt, z której będzie się dopiero wywodzil cGracz, cWrog, cSolids, cDestructible itd.
Ale nie widzę sensu pchania ich butem do jednego wektora, bo jak ja zrobie test kolizji? jak będę mieć wszystko w jednym wektorze, to skąd będę wiedział co jest wrogiem, a co beczką? Wiadomo z góry, że beczka po wieki wieków będzie stała gdzie stoi i nigdy nie przytuli się do ściany (na moim etapie nie mam ambicji tworzenia "ruchomego" środowiska) Dlatego wlasnie mam osobne wektory (będę miał). Będę robił test kolizji między wektorami cWrogowie i cSolids, ale już między cSolids a cDestructible nie.
to że std::vector wypełniony wskaźnikami działa to już wiem :] Pisałem kiedyś z kumplem asteroidsy... i tam właśnie miałem główną klasę cAsteroida z której wywodziły się cAstWybuchowa, cToxic itd. I one wszystkie były w jednym wektorze. w sumie mogę tu wrzucić - projektu nie dokończyliśmy i chyba już nie dokończymy :]
Tak swoją drogą - jak się pochwalić gotowym exekiem? (w sensie, gdzie polecacie wrzucić, żeby podlinkować)
I jeszcze jedno - temat trzeba by chyba podzielić - bo na pytanie o wskaźnik odpowiedź już uzyskałem, a zaczynamy dryfować w bliżej nieokreślonym kierunku (co zresztą całkiem przyjemne jest ;] )
|
|
1 « 2 » |