Skip to main content
gamedev.net
🔒 Locked

Inheritance and constructors

Started by Aargyle Apr 29, 2003 at 7:52 AM 11 replies 1k views
Original Post
Aargyle
Aargyle
Ok I need to pass parameters to a class constructor through its subclass constructor. How the HECK do I do that. I''ve been all over my C++ book and it doesn''t say. Basically I have this: class object { object( int x, int y, int z); }; class door : object { door( int x, int y, int z, int height, int width ); } And I need door to send the xyz to object... ew? Or should I just do the same code from object, in doors constructor? I think the default object constructor will override this though, when it sets everything to zero Thanks!
rypyr
rypyr

door::door(int x, int y, int z, int height, int width) : object(x,y,z) {
// set height and width here...
}
Arild Fines
Arild Fines

  
class door : object
{
door( int x, int y, int z, int height, int width ) : object( x, y, z )
{;}
}




"To assert that the earth revolves around the sun is as erroneous as to claim that Jesus was not born of a virgin."
-- Cardinal Bellarmine
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Aargyle
Aargyle
Woah excellent. Do I need to explicity send x,y,z over or does that definition take care of it for me?

Thanks btw!
rypyr
rypyr
You need to explicitly specify the variables to send down" to the object class. i.e. it would also be valid C++ to do this:


door::door(int x, int y, int z, int height, int width) : object(width, height, y) {
}

C++ can''t read your mind

Regards,
Jeff
drslush
drslush
Really? :-O That explains why my compiler has yet to get me that ice cream cone I have been asking for telepathically...
Aargyle
Aargyle
Ok I got that working, now I have a new question!

When I try to access a public member of the base class through the subclass, the compiler says no!

I get:
cannot access public member declared in class 'Object'

when I try:
door d;
int z = d->x;

Am I losing my mind? Its public right?

[edited by - aargyle on April 29, 2003 1:10:42 PM]
Kyo
Kyo
quote:
Original post by rypyr

door::door(int x, int y, int z, int height, int width) : object(x,y,z) {
// set height and width here...
}




Just wondering what''s the difference between door::door(params) : object(x, y, z) { } and door::door(params) { object(x, y, z); }

I use the second method as it''s clearer for me to read but is there some advantage to using : before the opening brackets?
elevator2
elevator2
quote:
Original post by Aargyle
Ok I got that working, now I have a new question!

When I try to access a public member of the base class through the subclass, the compiler says no!

I get:
cannot access public member declared in class ''Object''

when I try door->x

Am I losing my mind? Its public right?


class object
{
public:
int x,y,z;
object(int _x,int _y,int _z) : x(_x),y(_y),z(_z)
{}
};


class door : public Object
{
public: int width,height;
door( int x, int y, int z, int height, int width ) : object( x, y, z )
{//put initialization of width and height here
}
};

the keyword public ? you haven''t specified its !!!
Good luck !




CraZeE
CraZeE
kyo: i dun think u can call object(x,y,z) in the body of the constructor coz that would mean construction has already taken place. the idea of using the initializer list is to initialized values BEFORE the body of the function is called. in fact, if i'm not mistaken, the base class must be initialized before the child class.. which implies that the base class constructor can only be called in the initializer list.



[edited by - crazee on April 29, 2003 1:20:49 PM]
To learn, we share... Give some to take some -
Aargyle
Aargyle
Ah ok, I need the public in

class door : public Object


Thanks much! This is making my head swim, lol. I knew all this 3 years ago when I took my C++ class, and now when I come back to it its all gone! Mwahaha.

I do remember calling constructors in the body of a constructor though. Like

door::door(int x, int y, int z, int height, int width)
{
Object(x,y,z);
...
...
}

But I don''t know if its correct!
rypyr
rypyr
quote:
Original post by Kyo
Just wondering what''s the difference between door::door(params) : object(x, y, z) { } and door::door(params) { object(x, y, z); }

I use the second method as it''s clearer for me to read but is there some advantage to using : before the opening brackets?



Yes, the advantage is it will work

Calling Object(x,y,z) in the constructor means you are creating a temporary Object that is discarded at the end of the Door constructor. Have you ever verified your base members after doing it? What you''ll notice is that your base members will have whatever values your default constructor initializes them to and x,y,z will not have been set correctly.

You should also always use initializer lists for member variables where possible and just get used to the syntax.

Unfortunately, I think Java has a slightly different syntax where you use super() to call the constructor explicitly so it can potentially get confusing.

Regards,
Jeff

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.