Jump to content
  • Advertisement
Sign in to follow this  
Ryback

C++ Constructor Question

This topic is 5018 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings, Sorry if this is too simple of a question. Basically I am trying to understand a PLIB source file. ssgaTeapot is a class inherited from ssgaShape. Now, the constructor is defined as this: ssgaTeapot::ssgaTeapot(int nt):ssgaShape (nt){type=ssgaTypeTeapot();regenerate();} What does this mean exactly? I understand that the body is calling 2 functions. The part I don't exactly get is the ":ssgaShape (nt)" Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
It's an initializer list, normally used to initialize const variables in classes because they cannot be initialized in the body (they're const), and simply sets the variable to whatever is in parenthesis. So for this example ssgaShape is a variable of the class and is being initialized to the int nt.

Share this post


Link to post
Share on other sites
It determines which constructor overload of ssgaShape to use. If it's omitted, it attempts to use the default constructor. If ssgaShape has no default constructor, then it must be explicitly specified as shown. Else how would you specify what gets passed in?

Hope this helps.

Share this post


Link to post
Share on other sites

ssgaTeapot::ssgaTeapot(int nt)
:ssgaShape (nt)
{
type=ssgaTypeTeapot();
regenerate();
}



ssgaTeapot inherits from ssgaShape, and it's just calling ssgaShape's constructor... Is there some other way to look at this?

Share this post


Link to post
Share on other sites
Quote:
Original post by NoahAdler
It determines which constructor overload of ssgaShape to use. If it's omitted, it attempts to use the default constructor. If ssgaShape has no default constructor, then it must be explicitly specified as shown. Else how would you specify what gets passed in?

Hope this helps.


So if I define an object of type ssgaTeapot, it will call the constructor of ssgaShape AND the constructor of ssgaTeapot (which is defined in the body)?

Share this post


Link to post
Share on other sites
Quote:
So if I define an object of type ssgaTeapot, it will call the constructor of ssgaShape AND the constructor of ssgaTeapot (which is defined in the body)?


Yep, assuming ssgaTeapot inherits from ssgaShape. For instance the following program:


#include <iostream>

struct Base {
Base() { std::cout<<"Base()\n"; }
virtual ~Base() { std::cout<<"~Base()\n"; }
};

struct Derived : public Base {
Derived() { std::cout<<"Derived()\n"; }
virtual ~Derived() { std::cout<<"~Derived()\n"; }
};

int main() {
Derived d;
}





The output will be

Base()
Derived()
~Derived()
~Base()


This is standard inheritance rules. Base is responsible for initializing its own data, so its constructor needs to be called.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!