Click here to Skip to main content
15,914,905 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionLong Running Program Pin
BobInNJ24-Dec-08 16:25
BobInNJ24-Dec-08 16:25 
AnswerRe: Long Running Program Pin
Liu Xia24-Dec-08 17:22
Liu Xia24-Dec-08 17:22 
AnswerRe: Long Running Program Pin
Ravi Bhavnani24-Dec-08 18:42
professionalRavi Bhavnani24-Dec-08 18:42 
AnswerRe: Long Running Program Pin
dehseth25-Dec-08 19:08
dehseth25-Dec-08 19:08 
GeneralRe: Long Running Program Pin
Ravi Bhavnani26-Dec-08 11:35
professionalRavi Bhavnani26-Dec-08 11:35 
QuestionNeed help for numericupdwon Pin
sai5624-Dec-08 15:34
sai5624-Dec-08 15:34 
AnswerRe: Need help for numericupdwon Pin
Code-o-mat25-Dec-08 6:52
Code-o-mat25-Dec-08 6:52 
QuestionHow to write assignment operator for a class with constant member variable? Pin
ComplexLifeForm24-Dec-08 8:07
ComplexLifeForm24-Dec-08 8:07 
Hi,

I have a class with constant member variable. As constant member variables MUST be initialized in the constructor initializer list, how do I write an assignment operator for such a class. E.g.

class MyClass
{
public:
MyClass() : m_ConstantMember(0) - (1)
{}

MyClass(int intvar) : m_ConstantMember(intvar) - (2)
{}

MyClass(MyClass const& rhs) - (3)
: m_ConstantMember(rhs.m_ConstantMember)
{}

MyClass& operator = (MyClass const& rhs) -- (4)
{
if(this != &rhs)
{
m_ConstantMember = rhs.m_ConstantMember; -------- (4a)

//memcpy((void*)&m_ConstantMember,
(void*)(int*)&rhs.m_ConstantMember,
sizeof(m_ConstantMember)) ------------ (4b)
}
return *this;
}

private:
int const m_ConstantMember;
};

The above class has one constant member variable of type int. This variable MUST be initialized in the constructor initializer list. This is achieved in the above class by
1. No argument constructor
2. One argument constructor
3. Copy Constructor

For the assignment operator, if I write a statment as in 4a, the compiler complains. However if I write the statement as in 4b, the compiler is happy and I get the desired result.

So my query is whether the statement 4b is the correct way of writing an assignment operator for a class with constant member variable? If not can someone please tell me the correct way of writing the assignment operator.

Thanks and Regards.
Confused | :confused: Confused | :confused:
AnswerRe: How to write assignment operator for a class with constant member variable? Pin
Code-o-mat24-Dec-08 9:13
Code-o-mat24-Dec-08 9:13 
AnswerRe: How to write assignment operator for a class with constant member variable? Pin
Stuart Dootson24-Dec-08 13:15
professionalStuart Dootson24-Dec-08 13:15 
GeneralRe: How to write assignment operator for a class with constant member variable? Pin
ComplexLifeForm25-Dec-08 4:54
ComplexLifeForm25-Dec-08 4:54 
QuestionImage problem ? Pin
Le@rner24-Dec-08 0:24
Le@rner24-Dec-08 0:24 
AnswerRe: Image problem ? Pin
Naveen24-Dec-08 0:34
Naveen24-Dec-08 0:34 
GeneralRe: Image problem ? Pin
Le@rner24-Dec-08 0:42
Le@rner24-Dec-08 0:42 
GeneralRe: Image problem ? Pin
Naveen24-Dec-08 0:48
Naveen24-Dec-08 0:48 
GeneralRe: Image problem ? Pin
Hamid_RT24-Dec-08 0:51
Hamid_RT24-Dec-08 0:51 
GeneralRe: Image problem ? Pin
Code-o-mat24-Dec-08 6:03
Code-o-mat24-Dec-08 6:03 
AnswerRe: Image problem ? Pin
KASR124-Dec-08 1:47
KASR124-Dec-08 1:47 
Questionmedia streaming on a network! Pin
Mustapha Rédouane23-Dec-08 23:34
Mustapha Rédouane23-Dec-08 23:34 
AnswerRe: media streaming on a network! Pin
Naveen24-Dec-08 0:25
Naveen24-Dec-08 0:25 
AnswerRe: media streaming on a network! Pin
Varghese Paul M24-Dec-08 0:25
Varghese Paul M24-Dec-08 0:25 
AnswerRe: media streaming on a network! Pin
Hamid_RT24-Dec-08 0:58
Hamid_RT24-Dec-08 0:58 
QuestionPrint Dialog "Print To File" Pin
Paresh Chitte23-Dec-08 22:41
Paresh Chitte23-Dec-08 22:41 
QuestionHow to convert bianry data to Base64 Pin
Dark_eye23-Dec-08 21:33
Dark_eye23-Dec-08 21:33 
AnswerRe: How to convert bianry data to Base64 Pin
Richard Andrew x6424-Dec-08 5:19
professionalRichard Andrew x6424-Dec-08 5:19 

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.