S
subramanian
Consider the following program:
#include <iostream>
#include <string>
class Member {
int x;
int y;
public:
Member(int argx, int argy);
Member(const Member &m);
void operator=(const Member &m);
};
Member::Member(int argx, int argy)
{
x = argx;
y = argy;
return;
}
Member::Member(const Member &m)
{
x = m.x;
y = m.y;
std::cout << "from copy ctor of Member class : x = " << x << " y = "
<< y << '\n';
return;
}
void Member:perator=(const Member &m)
{
x = m.x;
y = m.y;
std::cout << "from operator= : x = " << x << " y = " << y << '\n';
return;
}
class Test {
public:
Test(const Member &argm1, const Member &argm2, const std::string
&val);
private:
std::string str;
Member member1;
Member member2;
};
Test::Test(const Member &argm1, const Member &argm2, const std::string
&val)
{
str = val;
member1 = argm1;
member2 = argm2;
std::cout << "from one arg ctor : " << str << '\n';
return;
}
int main(void)
{
Member argm1(10, 20);
Member argm2(100, 200);
Test tmp(argm1, argm2, "tmp object");
return 0;
}
If I compile this program with VC++ 2005 Express edition, it gives
compilation error at the line
Test::Test(const Member &argm1, const Member &argm2, const std::string
&val)
saying
'Member' : no appropriate default constructor available
Similar error occurs with g++ also.
If I change this ctor to
Test::Test(const Member &argm1, const Member &argm2, const std::string
&val) : member1(argm1), member2(arg2)
{
str = val;
std::cout << "from one arg ctor : " << str << '\n';
return;
}
then the compilation error goes. Though str is of class type
std::string, str = val is accepted. However the same does not hold for
class type Member for member1 = argm1 and member2 = argm2.
What is the difference ?
#include <iostream>
#include <string>
class Member {
int x;
int y;
public:
Member(int argx, int argy);
Member(const Member &m);
void operator=(const Member &m);
};
Member::Member(int argx, int argy)
{
x = argx;
y = argy;
return;
}
Member::Member(const Member &m)
{
x = m.x;
y = m.y;
std::cout << "from copy ctor of Member class : x = " << x << " y = "
<< y << '\n';
return;
}
void Member:perator=(const Member &m)
{
x = m.x;
y = m.y;
std::cout << "from operator= : x = " << x << " y = " << y << '\n';
return;
}
class Test {
public:
Test(const Member &argm1, const Member &argm2, const std::string
&val);
private:
std::string str;
Member member1;
Member member2;
};
Test::Test(const Member &argm1, const Member &argm2, const std::string
&val)
{
str = val;
member1 = argm1;
member2 = argm2;
std::cout << "from one arg ctor : " << str << '\n';
return;
}
int main(void)
{
Member argm1(10, 20);
Member argm2(100, 200);
Test tmp(argm1, argm2, "tmp object");
return 0;
}
If I compile this program with VC++ 2005 Express edition, it gives
compilation error at the line
Test::Test(const Member &argm1, const Member &argm2, const std::string
&val)
saying
'Member' : no appropriate default constructor available
Similar error occurs with g++ also.
If I change this ctor to
Test::Test(const Member &argm1, const Member &argm2, const std::string
&val) : member1(argm1), member2(arg2)
{
str = val;
std::cout << "from one arg ctor : " << str << '\n';
return;
}
then the compilation error goes. Though str is of class type
std::string, str = val is accepted. However the same does not hold for
class type Member for member1 = argm1 and member2 = argm2.
What is the difference ?