Standard c++ help

Started by
7 comments, last by SiCrane 11 years, 5 months ago
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:


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

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


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.
Advertisement
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 this to a member's constructor. The member's constructor is executed before Application's constructor, so the Application object you're passing to the Airplane'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 Airplane have to know about the Application?

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.

This changes with C++11, which allows brace-or-equal-initializers 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.

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 this to a member's constructor. The member's constructor is executed before Application's constructor, so the Application object you're passing to the Airplane'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 Airplane have to know about the Application?


[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.
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.

[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.

This changes with C++11, which allows brace-or-equal-initializers 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 this is indeed allowed, and that makes sense also, so there you go: +1 for that.
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).
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.
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.

This topic is closed to new replies.

Advertisement