How do you initialize a reference type in default constructor?

Started by
16 comments, last by Antheus 14 years, 5 months ago
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
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.
Remove the default constructor.

Edit: too slow!
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
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.
Just to answer your question, you can initialize reference variable
like you would initialize const variables, in a initializer list.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
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.hclass A{   A(B b)   {     this.b = b;   }   B b;}

//B.hclass 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
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?
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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

This topic is closed to new replies.

Advertisement