Create, delete and recreate same object with global visibility

Hello. I need some help to point me on the right direction to do something like this:

I have 2 similar classes ( Ortho and Frustum ) and this is what I would like to do:

in function X1, create OBJ1 from Ortho class
in function X2, call member OBJ1.move
in function X3, delete OBJ1

in function X1, create OBJ1 from Frustum class
in function X2, call member OBJ1.move
in function X3, delete OBJ1

So, I suppose OBJ1 should have global visibility, sometimes as an object from class Ortho and sometimes from Frustum.

Is that possible ?

Thank you
Marcio
Last edited on
If Ortho and Frustum are derived from the same class or one derives from the other, you can use a pointer to the base class and polymorphism
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.

Thanks.
An example of polymorphism (you probably wouldn't do this but...):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//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;
}
How to apply polymorphism in your case:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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

( Polymorphism could be a scary word but isn't a hard concept, if you need a tutorial check this: http://www.cplusplus.com/doc/tutorial/polymorphism/ )
Bazzy, what if things are more like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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 );
    else
	if 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;
}


Thank you
Marcio
Last edited on
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)

so the code should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 
Last edited on
Great! It's working. Thank you.

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();.
Last edited on
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.

Any suggestion ?

Thank you
Regards
Marcio


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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)'
Did you put the semicolons after the class closing brace?
1
2
3
4
class Base3D
{
    //...
}; // <--- 
Topic archived. No new replies allowed.