Jump to content

  • Log In with Google      Sign In   
  • Create Account

Standard c++ help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1201

Like
1Likes
Like

Posted 31 October 2012 - 03:41 PM

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.

Edited by BaneTrapper, 31 October 2012 - 03:54 PM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8195

Like
4Likes
Like

Posted 31 October 2012 - 03:55 PM

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?

Edited by Brother Bob, 31 October 2012 - 03:57 PM.


#3 SiCrane   Moderators   -  Reputation: 9596

Like
4Likes
Like

Posted 31 October 2012 - 10:28 PM

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.

#4 BaneTrapper   Members   -  Reputation: 1201

Like
0Likes
Like

Posted 01 November 2012 - 03:37 AM

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?


so the Application object you're passing to the Airplane's constructor is not fully constructed yet

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, 01 November 2012 - 03:42 AM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#5 rip-off   Moderators   -  Reputation: 8217

Like
1Likes
Like

Posted 01 November 2012 - 04:11 AM

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.

#6 Brother Bob   Moderators   -  Reputation: 8195

Like
1Likes
Like

Posted 01 November 2012 - 05:26 AM


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.

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.

#7 SiCrane   Moderators   -  Reputation: 9596

Like
1Likes
Like

Posted 01 November 2012 - 08:11 AM

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

#8 Brother Bob   Moderators   -  Reputation: 8195

Like
1Likes
Like

Posted 01 November 2012 - 08:28 AM

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.

#9 SiCrane   Moderators   -  Reputation: 9596

Like
0Likes
Like

Posted 01 November 2012 - 12:04 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS