Jump to content
  • Advertisement
Sign in to follow this  
KenWill

C++ calling a class constructor in a function argument

This topic is 991 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

So we have this:

//SecondClass
class SecondClass
{  
 private:
  int x;
  int y;
 public:
    SecondClass(int x, int y)
    {
      this->x = x;
      this->y = y;
    }
};

//MainClass
class MainClass
{
 private:
 SecondClass secondClass;
 public:
   MainClass(SecondClass &secondClass)
   {
      //stuff
   }
};
//main function




int main()

{

    MainClass mainClass(SecondClass secondClass(1,4));

}

I'm a little confused about the logic behind how can we call SecondClass' constructor as the argument. That is the moment when we create the object for the class? If yes, can i do this for other functions as well? I kinda answered my own question by asking this but i'm not sure..Can we call the constructor later in the code? For example i create my object now and later i call the constructor?

Share this post


Link to post
Share on other sites
Advertisement

There will always be the default constructor called for member objects. You could do it by declaring your secondClass member as a (smart-) pointer and allocating it at a later point in time, but unless you have a good reason to do it, you should stay with a direct member. Also, your code in main is not quite right:

  MainClass mainClass(SecondClass(1,4));

The way to directly call the (copy-) constructor for your member is to use the initializer-list of the constructor:

public:
  MainClass(SecondClass &secondClass)
    : secondClass(secondClass) {
    // stuff
  }

Share this post


Link to post
Share on other sites

 

There will always be the default constructor called for member objects. You could do it by declaring your secondClass member as a (smart-) pointer and allocating it at a later point in time, but unless you have a good reason to do it, you should stay with a direct member. Also, your code in main is not quite right:

  MainClass mainClass(SecondClass(1,4));

The way to directly call the (copy-) constructor for your member is to use the initializer-list of the constructor:

public:
  MainClass(SecondClass &secondClass)
    : secondClass(secondClass) {
    // stuff
  }

Is there a difference between using two variables to hold the values i need and doing this?

And the compiler knows that SecondClass has been declared inside the class, and thats why i'm not given an error with that? I'm confused...can you explain me the logic behind it?

Edited by KenWill

Share this post


Link to post
Share on other sites
'SecondClass' has not been declared inside the MainClass.

'secondClass' has been declared to be member data of type 'SecondClass' inside MainClass. Obviously when you say something contains something, you should be able to use it.

Share this post


Link to post
Share on other sites

Are you saying the code was compiling that way? That sounds unlikely to me...

 

First of all you have a syntax error in your main function (the one I showed you). If SecondClass had a default constructor (which it doesn't have, because you declared a non-default constructor manually), then the constructor of MainClass would have implicitly called the default constructor of SecondClass (which would have left x and y uninitialized because they are not objects), then it would just have forgotten about the SecondClass reference you passed to the constructor and do nothing with it.

 

But as I said, I don't think you have ever compiled that code.

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!