If they are different objects (as seems to be the case), why not just give them different names? If you delete the object, then any data related to it is lost so there I see no reason for having those two objects be the "same." If you really want to, you can make OBJ1 a void pointer and cast it to point to the object you want.
Bazzy: yes, they are derived from a same Base3D class. This word "polymorphism" at first scared me but I will give a try to understand it. Could you point me some simple example to start over ?
Zhuge: The reason to use the same name is because in function X2 there are many calls to OBJ1. Change from the classes will happen sometimes and there are few data to keep between them.
I tried the void pointer but it did not work. Will try again.
//assume includes etc
class Person {
public: //for simplicity
string name;
int age;
}
class DerivedPerson : public Person {
public:
bool dead;
}
string getName(Person* guy) {
return guy->name;
}
int main() {
Person* FIRST = new Person();
FIRST->name = "Joe";
DerivedPerson* SECOND = new DerivedPerson();
SECOND->name = "Bob";
//getName() will work with both classes, even though it only takes a pointer to the base class
//because of polymorphism (the derived class can be cast to the base class)
std::cout<<getName(FIRST)<<" "<<getName(SECOND)<<std::endl;
return 0;
}
class Base3D
{
//stuff
virtual TYPE move ( ARGUMENTS );//Notice: virtual
};
class Ortho : public Base3D
{
//something
};
class Frustum : public Base3D
{
//something else
};
Base3D *OBJ1;//I used the name from your 1st post but this is a pointer, not an object
TYPE X1 ( ARGUMENTS )
{
if ( YouWantCreate_Ortho_ )
OBJ1 = new Ortho ( CONSTRUCTOR_ARGUMENTS );
else
OBJ1 = new Frustum ( CONSTRUCTOR_ARGUMENTS );
}
TYPE X2 ( ARGUMENTS )
{
OBJ1->move();
}
TYPE X3 ( ARGUMENTS )
{
delete OBJ1;
}
Notice that once allocated the new object, you will be able to access the Base3D members and virtual methods will act as the implementations in the derived classes
class Base3D
{
// something
// virtual TYPE move ( ARGUMENTS );//Notice: virtual
};
class Ortho : public Base3D
{
TYPE move ( ARGUMENTS ); // this "move" has one kind of math
};
class Frustum : public Base3D
{
TYPE move ( ARGUMENTS ); // this "move" has some other kind of math
};
Base3D *OBJ1; //I used the name from your 1st post but this is a pointer, not an object
TYPE X1 ( ARGUMENTS )
{
if ( YouWantCreate_Ortho_ )
if OBJ1 is created, execute X3, then ...
OBJ1 = new Ortho ( CONSTRUCTOR_ARGUMENTS );
elseif OBJ1 is created, execute X3 then ...
OBJ1 = new Frustum ( CONSTRUCTOR_ARGUMENTS );
}
TYPE X2 ( ARGUMENTS )
{
OBJ1->move(); // move things according current environment (Ortho or Frustum)
}
TYPE X3 ( ARGUMENTS )
{
delete OBJ1;
}
You need to have a virtual function in the base class. If you don't need one you can do it two ways:
1- Make the function in Base3D doing nothing virtual TYPE move ( ARGUMENTS ) {}
2- Make Base3D an abstract class virtual TYPE move ( ARGUMENTS ) = 0;
Notice that if you make Base3D an abstract class you can't directly create objects of that type but you will be able to create objects from derived classed which gave a body to the method 'move'
(In my previous post i forgot to put 'move' in the derived classes)
class Base3D
{
// something
#if YOU_WANT_AN_ABSTRACT_CLASS
virtual TYPE move ( ARGUMENTS ) =0;
#else
virtual TYPE move ( ARGUMENTS ) { };
#end
};
class Ortho : public Base3D
{
TYPE move ( ARGUMENTS ); // this "move" has one kind of math
};
class Frustum : public Base3D
{
TYPE move ( ARGUMENTS ); // this "move" has some other kind of math
};
//etc
How can I create these "non-virtual destructor" gcc is warning me ?
Marcio
1 2 3 4 5 6 7 8 9 10 11 12 13
g++ -O3 -Wall -c -fmessage-length=0 -ox2.o ..\x2.cpp
In file included from ..\x2.cpp:1:
..\OrthoBox.h:15: warning: `class OrthoBox' has virtual functions but non-virtual destructor
In file included from ..\x2.cpp:2:
..\FrusBox.h:16: warning: `class FrusBox' has virtual functions but non-virtual destructor
g++ -O3 -Wall -c -fmessage-length=0 -oOrthoBox.o ..\OrthoBox.cpp
In file included from ..\OrthoBox.cpp:2:
..\OrthoBox.h:15: warning: `class OrthoBox' has virtual functions but non-virtual destructor
g++ -O3 -Wall -c -fmessage-length=0 -oFrusBox.o ..\FrusBox.cpp
In file included from ..\FrusBox.cpp:1:
..\FrusBox.h:16: warning: `class FrusBox' has virtual functions but non-virtual destructor
The compiler is complaining because you have non-virtual destructors.
If you have destructors in your derived classes you should set the base class destructor to be virtual: virtual ~Base3D();.
I must be doing something wrong in the structure below, but I can not find what.
I made all possible turn on/off combination regard the constructors/destructors but the error is still there.
It just compile and link 100% ok if I totally remove constructors/destructors.
class Base3D
{
public:
Base3D();
virtual ~Base3D();
}
Base3D::Base3D() { }
Base3D::~Base3D() { }
class OrthoBox : public Base3D
{
public:
OrthoBox();
~OrthoBox();
}
OrthoBox::OrthoBox() { }
OrthoBox::~OrthoBox() { }
class FrusBox : public Base3D
{
public:
FrusBox();
~FrusBox();
}
FrusBox::FrusBox() { }
FrusBox::~FrusBox() { }
OrthoBox.o:OrthoBox.cpp:(.text+0xe): undefined reference to `Base3D::Base3D()'
FrusBox.o:FrusBox.cpp:(.text+0xe): undefined reference to `Base3D::Base3D()'
<< plus several msgs similar this one >>
Base3D.o:Base3D.cpp:(.rdata$_ZTV6Base3D[vtable for Base3D]+0x10): undefined reference to `Base3D::start(double, double, double)'