Click here to Skip to main content
15,906,816 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: simple C function question Pin
jmkhael24-Feb-03 5:36
jmkhael24-Feb-03 5:36 
GeneralCTime question Pin
Anonymous24-Feb-03 4:53
Anonymous24-Feb-03 4:53 
GeneralRe: CTime question Pin
João Paulo Figueira24-Feb-03 5:09
professionalJoão Paulo Figueira24-Feb-03 5:09 
GeneralRe: CTime question Pin
jhwurmbach24-Feb-03 5:09
jhwurmbach24-Feb-03 5:09 
GeneralRe: CTime question Pin
Alvaro Mendez24-Feb-03 5:25
Alvaro Mendez24-Feb-03 5:25 
GeneralRe: CTime question Pin
Anonymous24-Feb-03 5:27
Anonymous24-Feb-03 5:27 
GeneralRe: CTime question Pin
Dave_24-Feb-03 9:39
Dave_24-Feb-03 9:39 
GeneralCopy constructor Pin
Jerome Conus24-Feb-03 4:27
Jerome Conus24-Feb-03 4:27 
Hi !

I was trying to implement a 'Rational Numbers' class when I came across something strange that I never noticed before and that I cannot explain.

Here is my class :

class CRational
{
private:
	long m_lNumerator;
	long m_lDenominator;

public:
	CRational(long lNum=0, long lDenom=1)
	{
		m_lNumerator=lNum;
		m_lDenominator=lDenom;
	};

	CRational(const CRational& objNombre)
	{
		m_lNumerator=objNombre.m_lNumerator;
		m_lDenominator=objNombre.m_lDenominator;
	};

	CRational& operator=(const CRational& objNombre)
	{
		m_lNumerator=objNombre.m_lNumerator;
		m_lDenominator=objNombre.m_lDenominator;
		return *this;
	};

	long GetNumerator() { return m_lNumerator; };
	long GetDenominator() { return m_lDenominator; };
};


This class is working fine, but I'm wondering why, in the copy constructor, these lines :
m_lNumerator=objNombre.m_lNumerator;
m_lDenominator=objNombre.m_lDenominator;

are allowed by the compiler, because I'm accessing private members of an object.

Well, I tried anyway to change the copy constructor to this :
CRational& operator=(const CRational& objNombre)
{
    m_lNumerator=objNombre.GetNumerator();
    m_lDenominator=objNombre.GetDenominator();
    return *this;
};

...which is refused by the compiler :
error C2662: 'GetNumerator' : cannot convert 'this' pointer from 'const class CRational' to 'class CRational &'


It compiles fine if I remove the 'const' in the declaration of the constructor, which seems strange to me, because I'm not modifying the object 'objNombre', just using its accessors....

Anyone could explain me what's going on ??

Thanks !
Jerome
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 
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 

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.