| | |
Const Question - Why is this happening?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
this is nothing specific to constructors or passing parameters to functions.
it is just the application of the C++ rules for resolving a call to an overloaded function.
in your snippet:
the copy constructor that takes a non-const object as argument is used to make copies of non-const objects.
and the copy constructor that takes a const object as argument is used to make copies of const objects.
it is just the application of the C++ rules for resolving a call to an overloaded function.
c++ Syntax (Toggle Plain Text)
#include <iostream> void foobar( int& arg ) { std::cout << "foobar( int& )\n" ; } void foobar( const int& arg ) { std::cout << "foobar( const int& )\n" ; } int main() { int i = 7 ; foobar(i) ; // foobar( int& arg ) : exact match // foobar( const int& ) : conversion from int& to const int& // exact match preferred over conversion const int j = 7 ; foobar(j) ; // foobar( const int& arg ) : exact match // foobar( int& arg ) can't be called at all ( no conversion ) }
in your snippet:
c++ Syntax (Toggle Plain Text)
int main () { MyClass f(4); Func (f); // make copy of object f. f is a modifiable lvalue // overload resolves to exact match : MyClass::MyClass( MyClass& ) const MyClass& g = f ; Func (g); // make copy of object g. g is not a modifiable lvalue // call MyClass::MyClass( const MyClass& ) to make the copy }
and the copy constructor that takes a const object as argument is used to make copies of const objects.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Delete or Remove Array Element
- Next Thread: problems with reading random access line from a file
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






