By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

Difference between initialisation and assignment?

P: n/a
Is there any difference between:

foo A = B;

and

foo A;
A = B;

Specifically, I have a case where B is a function call which returns a
templated class. The first case gives a syntax error, but the second
case works. It appears to work because the assignment calls a type
conversion operator for the templated class.

Thanks -

AL
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Andy Lomax wrote:

Is there any difference between:

foo A = B;

and

foo A;
A = B;


The first is initialization.
The second is assignment.

For intialization a constructor is used.
For assignment the op= is used.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

P: n/a
Jai
For intialization(foo A = B;) a copy constructor is used.
For assignment(A = B;) the op= is used.
Jai

Jul 23 '05 #3

P: n/a
class A
{
private:
int m_x;
public:
A( int x ) : m_x( x )
{
}
};

A a1 = 1; // should compile because constructor is not explicit
A a2; a2=2; // error. A does not have default constructor.

class B
{
private:
int m_x;
public:
B( int x ) : m_x( x )
{
}
B() : m_x(0)
{
}
};

const B b1=1; // should compile
const B b2; b2=2; // error, cannot assign b2 because it's const

class C
{
private:
int m_x;
public:
explicit C( int x ) : m_x( x )
{
}
B() : m_x(0)
{
}
};

C c1 = 1; // error, implicit cast
C c2; c2 = 2; // also an error
C c3( 3 ); // correct

Jul 23 '05 #4

P: n/a
Earl Purple wrote:


What is the point you are trying to make?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.