473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Difference between initialisation and assignment?

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
4 1490
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
Jai
For intialization(foo A = B;) a copy constructor is used.
For assignment(A = B;) the op= is used.
Jai

Jul 23 '05 #3
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
Earl Purple wrote:


What is the point you are trying to make?

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
2
by: Tim | last post by:
Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage?...
3
by: sushant | last post by:
hi all, whats the difference between assignement and initialisation in C. is assignement related with scanf() and initialisation with (int x=10;) sushant
7
by: pauldepstein | last post by:
A book I have describes a class called vector. It then explains (and I understand) the concept of a copy constructor, which is needed to interpret statements like vector b = a; But then I...
18
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
13
by: Frederick Gotham | last post by:
I have just been reading 8.5 in the Standard, and am trying to make sense of the different kinds of initialisations. Up until now, I thought of an object as either NOT being initialised (i.e....
5
by: kselvaakumar | last post by:
I have declared a global structure like this: struct { float a; float b; }static one; This is in a separate header file file1.h; Now how can i initialise those variables one.a and one.b in...
2
by: mukesh tiwari | last post by:
Hi everybody i want to know about array initialisation . Today i was solving a problem and here is my code #include<cstdio> #include<cstring> int l; int div(char *a,int d) { int k; char...
6
by: Taras_96 | last post by:
Hi everyone, The FAQ at http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 states that: "Consider the following constructor that initializes member object x_ using an initialization...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.