Click here to Skip to main content
16,008,719 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralCopy constructor Pin
Jerome Conus24-Feb-03 4:27
Jerome Conus24-Feb-03 4:27 
GeneralRe: Copy constructor Pin
João Paulo Figueira24-Feb-03 4:37
professionalJoão Paulo Figueira24-Feb-03 4:37 
GeneralRe: Copy constructor Pin
Jerome Conus24-Feb-03 4:43
Jerome Conus24-Feb-03 4:43 
GeneralRe: Copy constructor Pin
João Paulo Figueira24-Feb-03 4:50
professionalJoão Paulo Figueira24-Feb-03 4:50 
GeneralRe: Copy constructor Pin
Jerome Conus24-Feb-03 4:53
Jerome Conus24-Feb-03 4:53 
GeneralRe: Copy constructor Pin
João Paulo Figueira24-Feb-03 5:02
professionalJoão Paulo Figueira24-Feb-03 5:02 
GeneralRe: Copy constructor Pin
Jerome Conus24-Feb-03 5:06
Jerome Conus24-Feb-03 5:06 
GeneralRe: Copy constructor Pin
Alvaro Mendez24-Feb-03 4:49
Alvaro Mendez24-Feb-03 4:49 
Jerome Conus wrote:
This class is working fine, but I'm wondering why, in the copy constructor, these lines are allowed by the compiler, because I'm accessing private members of an object.

Yes, but the object is of the same class as "this". So the compiler allows it.


Jerome Conus wrote:
Anyone could explain me what's going on ??

objNombre is passed as const reference. "Const" means that it can't be changed, so you can only call member functions that are also declared const. Since GetNumerator and GetDenominator are not const member functions, they could technically be used to change the object's data (member variables). But since you passed objNombre as a const, then it's not legal. I hope this makes sense.

Anyway, you have two options to solve this:

1. Make objNombre not const. This will allow you to call const and non-const members from it. However, this is not the optimal solution.

2. Add const to the declaration of GetNumerator and GetDenominator:

long GetNumerator() const { return m_lNumerator; };	<br />
long GetDenominator() const { return m_lDenominator; };


This is the proper way to do things. Anytime a member function does not change the object's data, it should be made const so that it can be called by const objects, such as your objNombre.

Regards,
Alvaro



All you need in this life is ignorance and confidence, and then success is sure. -- Mark Twain
GeneralRe: Copy constructor Pin
Jerome Conus24-Feb-03 4:52
Jerome Conus24-Feb-03 4:52 
GeneralScrollbar HTMLView Pin
jeremysay24-Feb-03 4:15
jeremysay24-Feb-03 4:15 
GeneralRe: Scrollbar HTMLView Pin
HENDRIK R24-Feb-03 4:35
HENDRIK R24-Feb-03 4:35 
GeneralRe: Scrollbar HTMLView Pin
jeremysay24-Feb-03 5:02
jeremysay24-Feb-03 5:02 
GeneralRe: Scrollbar HTMLView Pin
HENDRIK R24-Feb-03 5:10
HENDRIK R24-Feb-03 5:10 
GeneralRe: Scrollbar HTMLView Pin
jeremysay24-Feb-03 5:24
jeremysay24-Feb-03 5:24 
Generaldialog Pin
dudic24-Feb-03 3:58
dudic24-Feb-03 3:58 
GeneralRe: dialog Pin
valikac24-Feb-03 9:25
valikac24-Feb-03 9:25 
GeneralA simple problem: cannot change project complie mode Pin
chen24-Feb-03 3:52
chen24-Feb-03 3:52 
GeneralRe: A simple problem: cannot change project complie mode Pin
TigerNinja_24-Feb-03 7:25
TigerNinja_24-Feb-03 7:25 
GeneralRe: A simple problem: cannot change project complie mode Pin
Anonymous24-Feb-03 13:45
Anonymous24-Feb-03 13:45 
GeneralDll importing question Pin
will138324-Feb-03 3:36
will138324-Feb-03 3:36 
GeneralRe: Dll importing question Pin
AlexO24-Feb-03 3:40
AlexO24-Feb-03 3:40 
GeneralRe: Dll importing question Pin
João Paulo Figueira24-Feb-03 3:41
professionalJoão Paulo Figueira24-Feb-03 3:41 
GeneralRe: Dll importing question Pin
will138324-Feb-03 3:52
will138324-Feb-03 3:52 
GeneralRe: Dll importing question Pin
João Paulo Figueira24-Feb-03 4:02
professionalJoão Paulo Figueira24-Feb-03 4:02 
GeneralRe: Dll importing question Pin
will138324-Feb-03 4:08
will138324-Feb-03 4:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.