Jump to content
  • Advertisement
Sign in to follow this  
chadsxe

How do you initialize a reference type in default constructor?

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

Kinda hard to explain but I am getting this error error C2758: 'Graphics::m_applicationReference' : must be initialized in constructor base/member initializer list
class Application;

class Graphics
{
	public:
		Graphics();
		Graphics(Application &applicationRef);
		Graphics(const Graphics &graphicsClass);
		Graphics& operator= (const Graphics &graphicsClass);
		~Graphics();
	private:
		Application &m_applicationReference;	// Ref to the application

};

Graphics::Graphics()
{
   // Error C2758 points to hear
   // How am I suppose to initialize a reference type?
}
Graphics::Graphics(Application &applicationRef)
: m_applicationReference(applicationRef)
{
}
Graphics::Graphics(const Graphics &graphicsClass)
: m_applicationReference(graphicsClass.m_applicationReference)
{
}
Graphics& Graphics::operator= (const Graphics &graphicsClass)
{
	// do the copy
	m_applicationReference = graphicsClass.m_applicationReference;
	// return the existing object
	return *this;
}
It appears to be telling me I need to initialize m_applicationReference in my Default Consttuctor. But I have no clue how to accomplish that. Any suggestion? Regards Chad

Share this post


Link to post
Share on other sites
Advertisement
If you use or plan to use the default constructor or you plan to use the assignment or copy constructor, you should use a pointer to the application instead. Otherwise remove the default constructor, assignment, and copy constructor.

This also doesn't do what you think it does (or could be fatal):

Graphics& Graphics::operator= (const Graphics &graphicsClass)
{
// do the copy
m_applicationReference = graphicsClass.m_applicationReference;
}




Use a pointer instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall
If you use or plan to use the default constructor or you plan to use the assignment or copy constructor, you should use a pointer to the application instead. Otherwise remove the default constructor, assignment, and copy constructor.

This also doesn't do what you think it does (or could be fatal):
*** Source Snippet Removed ***

Use a pointer instead.


So now you got me worried. What am I doing wrong with the operator= ?

You also suggest using pointers. How does this look.
Graphics::Graphics()
{
}
Graphics::Graphics(Application *applicationRef)
: m_applicationPointer(applicationRef)
{
}
Graphics::Graphics(const Graphics &graphicsClass)
: m_applicationPointer(graphicsClass.m_applicationPointer)
{
}
Graphics& Graphics::operator= (const Graphics &graphicsClass)
{
// do the copy
m_applicationPointer = graphicsClass.m_applicationPointer;
// return the existing object
return *this;
}
I am guessing there is still something wrong with the operator=

Any suggestion?

Regards

Chad

Share this post


Link to post
Share on other sites
The problem isn't the graphics class, but rather the big picture. Are you really planning on instantiating a graphics class, then initializing the application pointer/reference later? If so, then you must use a pointer. Are you really planning on copying a graphics class? If so, then you must use a pointer (see assignment operators, and references). Are you really planning on juggling and moving more than one graphics instance? If you answered no to all of these questions, then it is fine to use a reference, just remove the copy constructor, assignment operator, and the default constructor.

Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall
The problem isn't the graphics class, but rather the big picture. Are you really planning on instantiating a graphics class, then initializing the application pointer/reference later? If so, then you must use a pointer. Are you really planning on copying a graphics class? If so, then you must use a pointer (see assignment operators, and references). Are you really planning on juggling and moving more than one graphics instance? If you answered no to all of these questions, then it is fine to use a reference, just remove the copy constructor, assignment operator, and the default constructor.


O.k bear with me...I might not be understanding the best way to do this...

I am trying to store a reference/pointer to a class object in another class object. Lets say I ahve something like

//A.h
class A
{
A(B b)
{
this.b = b;
}

B b;
}

//B.h
class B
{
B();
A a; // How the heck do I initalize the A object with the the current B object
// I need to do this a(b) some place but I dont know how to.
}
What is throwing me for the curve is that I am declaring an A object in class B but I can't pass arguments to to the A objects constructor because it resides in my .h file. Is there a way to pass the argument to the constructor? I thought the only way to do that was the way I was going about it.

Does this make sense?

Regards

Chad

Share this post


Link to post
Share on other sites
Take a step back. The code you just posted has an B in every A, and a A in every B. This won't work - this will go on forever!

Did you miss out some &s to mark references?

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
Remove the default constructor.

Edit: too slow!
The default constructor can still be used but the only things you could initialise the reference with would be a global or static variable, or new object, or an object obtained through some other function call.
However the first two of those would defeat the point of having it as a reference, the third would give you unique instances rather than shared ones, and the fourth doesn't really give any bennefits either. So yeah, remove the default constructor, or reconsider pointers instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by mattd
Take a step back. The code you just posted has an B in every A, and a A in every B. This won't work - this will go on forever!

Did you miss out some &s to mark references?


Sorry I did not put them in there because I was not sure if I was going to be using a * or a & . I instead wrote that out in my text explanation.

Sorry for the confusion.

Regards

Chad

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!