943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1631
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 28th, 2008
1

Re: Const Question - Why is this happening?

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.
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. void foobar( int& arg )
  4. { std::cout << "foobar( int& )\n" ; }
  5.  
  6. void foobar( const int& arg )
  7. { std::cout << "foobar( const int& )\n" ; }
  8.  
  9. int main()
  10. {
  11. int i = 7 ;
  12.  
  13. foobar(i) ; // foobar( int& arg ) : exact match
  14. // foobar( const int& ) : conversion from int& to const int&
  15. // exact match preferred over conversion
  16.  
  17. const int j = 7 ;
  18. foobar(j) ; // foobar( const int& arg ) : exact match
  19. // foobar( int& arg ) can't be called at all ( no conversion )
  20.  
  21. }

in your snippet:
c++ Syntax (Toggle Plain Text)
  1. int main ()
  2. {
  3. MyClass f(4);
  4. Func (f); // make copy of object f. f is a modifiable lvalue
  5. // overload resolves to exact match : MyClass::MyClass( MyClass& )
  6.  
  7. const MyClass& g = f ;
  8. Func (g); // make copy of object g. g is not a modifiable lvalue
  9. // call MyClass::MyClass( const MyClass& ) to make the copy
  10. }
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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Sep 28th, 2008
0

Re: Const Question - Why is this happening?

Okay, thanks guys! That's very helpful. I'm going to mark this one solved.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Delete or Remove Array Element
Next Thread in C++ Forum Timeline: problems with reading random access line from a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC