Sign in to follow this  
BaneTrapper

Standard c++ help

Recommended Posts

BaneTrapper    1531
Hello.
I am trying to declare a class"Application" and inside this class"Application" make a object of class "Airplane" and "Airplane" constructor takes reference object of class "Application" how to make that?

Exzample:

[CODE]
"Airplane.h"
class Application;
class Airplane
{
public:
Airplane(Application& objApp);
int wings;
};

"Application.h"
#include "Airplane.h"
class Application
{
public:
Airplane objectofAirplane (*this);
};
[/CODE]

Youtube is crashing so hard. Cant get to tutorials, cant even see how to define this

Basically what i am trying to achieve is to call a function from class "Application" in "Airplanes constructor".
Now i don't think this is possible but... worth asking. Edited by BaneTrapper

Share this post


Link to post
Share on other sites
Brother Bob    10344
Assuming that is your exact code, then the primary problem is that you cannot both define and initialize members of a class like that. You have to initialize them in the constructor's initializer list.

The secondary problem, once the above is "solved", is that you should be very careful when you pass [i]this [/i]to a member's constructor. The member's constructor is executed before [i]Application[/i]'s constructor, so the [i]Application [/i]object you're passing to the [i]Airplane[/i]'s constructor is not fully constructed yet. I don't know if it is outright undefined, or simply just undefined to touch the parts that has yet to been initialized so that you can arrange the member carefully to avoid accessing uninitialized parts.

The tertiary problem is (or rather, may be) a design problem: why does the [i]Airplane [/i]have to know about the [i]Application[/i]? Edited by Brother Bob

Share this post


Link to post
Share on other sites
SiCrane    11839
[quote name='Brother Bob' timestamp='1351720559' post='4995961']
Assuming that is your exact code, then the primary problem is that you cannot both define and initialize members of a class like that. You have to initialize them in the constructor's initializer list.[/quote]
This changes with C++11, which allows [i]brace-or-equal-initializers[/i] for member variables. Though as the name suggests you need to use either braces or = in order to specify out of constructor initializers. However, not as many compilers as I would like support this syntax now.

Share this post


Link to post
Share on other sites
BaneTrapper    1531
[quote name='Brother Bob' timestamp='1351720559' post='4995961']
Assuming that is your exact code, then the primary problem is that you cannot both define and initialize members of a class like that. You have to initialize them in the constructor's initializer list.

The secondary problem, once the above is "solved", is that you should be very careful when you pass [i]this [/i]to a member's constructor. The member's constructor is executed before [i]Application[/i]'s constructor, so the [i]Application [/i]object you're passing to the [i]Airplane[/i]'s constructor is not fully constructed yet. I don't know if it is outright undefined, or simply just undefined to touch the parts that has yet to been initialized so that you can arrange the member carefully to avoid accessing uninitialized parts.

The tertiary problem is (or rather, may be) a design problem: why does the [i]Airplane [/i]have to know about the [i]Application[/i]?
[/quote]

[quote name='Brother Bob']
so the Application object you're passing to the Airplane's constructor is not fully constructed yet
[/quote]
That's why i stated i think its impossible this way.

Well the reason i was trying to pass it this was is the reason i need to call "Airplane::SetAirplane(All the variables);
Example: Am passing pointers to something like "MousePosX" and am thinking a way around from calling a Set function

I decided not to waste too much time here, a whole game needs to be made.
A good day sir's. Edited by BaneTrapper

Share this post


Link to post
Share on other sites
rip-off    10976
The fundamental design problem here is that the aeroplane is assuming too much knowledge of its environment. To decouple the classes, instead of having the aeroplane ask about the position of the mouse, let the application notify the aeroplane when the mouse changes.

Going even further, aeroplanes don't need to know about mice. They need trajectory changes or targets. So the application can just inform the aeroplane that the user has adjusted its orientation, or that the user has clicked on a new target, or whatever (depending on the nature of your game). This also makes it easier later on if you decide to support alternative input methods, for instance a joystick.

Share this post


Link to post
Share on other sites
Brother Bob    10344
[quote name='SiCrane' timestamp='1351744081' post='4996068']
[quote name='Brother Bob' timestamp='1351720559' post='4995961']
Assuming that is your exact code, then the primary problem is that you cannot both define and initialize members of a class like that. You have to initialize them in the constructor's initializer list.[/quote]
This changes with C++11, which allows [i]brace-or-equal-initializers[/i] for member variables. Though as the name suggests you need to use either braces or = in order to specify out of constructor initializers. However, not as many compilers as I would like support this syntax now.
[/quote]
I thought that way of initializing members was only allowed for compile-time or constant expressions, or perhaps even expressions with members that has been initialized already, since you cannot pass any parameters (unless you want to correct me again). That would make it useless in this particular case. But seems like [i]this[/i] is indeed allowed, and that makes sense also, so there you go: +1 for that.

Share this post


Link to post
Share on other sites
SiCrane    11839
I'm not sure what you mean by "cannot pass any parameters". If you are saying that you can't pass any of the parameters from a constructor to a brace-or-equal-initializer, then no, the you can't. The class member initializers are used in the default case, if no member initializers are used for that member in a constructor's member initialization list. However, that's not what the code in the OP was doing. Otherwise, class member initializers have no restrictions on the complexity of the expressions used as initializers for non-constexpr member variables that I can find in the standard (admittedly, the semantics for brace-or-equal-initializers are spread out over something like five different sections so I may I have missed one). In particular, member functions are explicitly fair game as it talks about how virtual functions called work just like other virtual functions called in constructors and the class is specifically noted to be a completed type within the scope of a brace-or-equal-initializer so any expression involving any of the class' member variables are also fair game (unfortunately, this also means members that haven't been constructed yet).

Share this post


Link to post
Share on other sites
Brother Bob    10344
As you said, I meant that you cannot (as I understood it) get any parameters from the constructor to the initializer. I just thought that there's not a lot you can do other than giving a default value, given that you cannot compute anything object-specific that needs object-specific parameters you typically pass to the constructor. However, they seem to be more a bit more flexible then I had first assumed.

Share this post


Link to post
Share on other sites
SiCrane    11839
I suppose you could pass parameters indirectly. You could have the constructor initialize a member variable using parameters and then reference that member variable in the initializers for other member variables. Ex: constructor initializes a count variable and the member initializer allocates memory based on that count. I wouldn't want to maintain that kind of code, but I don't think there's anything in the standard to prevent it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this