Page 1 of 1

const reference in ctor.. why?

Posted: Sat Jun 23, 2012 8:41 am
by REDDemon
Can anyone explain me why compiler (mingw) gives me errors if in the constructor I don't set a const reference?

wrong code

Code: Select all

 
//constructor
SomeClass::SomeClass(something & ref, foo * p) //compiler gives error but on 2nd parameter
{
 
}
 
returned error when I try to call constructor

Code: Select all

 
No matching function for call SomeClass::SomeClass(something & ref, foo *& p)
Candidates are...
 
No matching function for call SomeClass::SomeClass(something & ref, int)  //in this case null pointer is detected as "int" O_O
Candidates are...
 
correct code:

Code: Select all

 
//constructor
SomeClass::SomeClass(const something & ref, foo * p) //compile without errors.
{
 
}
 
I feel like I'm missing some fundamental thing. Tried to google and read FAQS but didn't find anything

Re: const reference in ctor.. why?

Posted: Sat Jun 23, 2012 8:56 am
by Mel
The error says that you are trying to use *&p as the second parameter. The compiler understands that it is a pointer to a reference, when you have to pass just a pointer. I can't tell really why using a const on the first parameter will help though, can you post actual code?

Re: const reference in ctor.. why?

Posted: Wed Jun 27, 2012 8:05 pm
by REDDemon
No special code needed for that.

following just don't compile unless I put a const reference in Foo's constructor

Code: Select all

 
class Bar;
 
class MyClass
{
    MyClass(){}
};
 
class Foo
{
public:
    Foo(MyClass & myFoo,Bar * bar)
    {
 
    }
};
 
class Bar
{
public:
 
    Bar()
    {
 
    }
};
 
 

Seems that is impossibile mixing references and pointers in the same contructor (unless the reference is const).
Anyway I found some readings that tells to avoid Non-const references in constructors (without explaining why :/)

Re: const reference in ctor.. why?

Posted: Thu Jun 28, 2012 1:56 am
by blAaarg
Anyway I found some readings that tells to avoid Non-const references in constructors (without explaining why :/)
Hmm, I don't know about mingw specifically, but perhaps they are saying that--"conceptually" speaking--constructors should just take external data for copying and not alter it.

I hadn't read that before but I suppose it makes sense, unless you're using a manipulator or function object or something. (I don't know if you would even want a functor to do that. It seems messy. I just meant that there may be exceptions to whatever that rule is.)

Re: const reference in ctor.. why?

Posted: Thu Jun 28, 2012 8:32 am
by CuteAlien
Your code above compiles here on MinGW (once I add a main()). And using non-const references in constructors should be all right - if you have references in your class this is even the only way to set them.

Re: const reference in ctor.. why?

Posted: Fri Jun 29, 2012 6:34 am
by REDDemon
CuteAlien wrote:Your code above compiles here on MinGW (once I add a main()). And using non-const references in constructors should be all right - if you have references in your class this is even the only way to set them.

that compile ?O_O i'll try to re-install. still don't compile here O_O thx for trying

Re: const reference in ctor.. why?

Posted: Fri Jun 29, 2012 10:12 am
by serengeor
Compiles for me too on mingw.