473,386 Members | 1,668 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.

Copy Constructor

Hello,

I have a class A. I have a copy constructor inside that class.

From the main function i am calling the copy constructor like below.


A a;
A b = a;

Now whenever the compiler comes to the statement A b=a. how does this work.

My doubt is whether the object b will be created first and a will be copied to b?

Regards,
Bhadri
Jun 27 '07 #1
5 1176
Hello,

I have a class A. I have a copy constructor inside that class.

From the main function i am calling the copy constructor like below.


A a;
A b = a;

Now whenever the compiler comes to the statement A b=a. how does this work.

My doubt is whether the object b will be created first and a will be copied to b?

Regards,
Bhadri
From your statements

A a;
A b = a;

It is clear that object a is created first. In the second statement object b is created by passing object "a" as a parameter to the copy constructor. It is converted in this fashion A b(a);

If you don't want this automatic conversion, then supply keyword "explict" for
the constructor so that it stops from such automatic conversions.

If you think, I have answered to your question then don't forget to close this
thread.

Regards,
Girish.
Jun 27 '07 #2
From your statements

A a;
A b = a;

It is clear that object a is created first. In the second statement object b is created by passing object "a" as a parameter to the copy constructor. It is converted in this fashion A b(a);

If you don't want this automatic conversion, then supply keyword "explict" for
the constructor so that it stops from such automatic conversions.

If you think, I have answered to your question then don't forget to close this
thread.

Regards,
Girish.
Hello,

Thanks for your reply.
Jun 27 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
This is not correct:
If you don't want this automatic conversion, then supply keyword "explict" for
the constructor so that it stops from such automatic conversions.
The example is an explicit conversion. Explicit means that onlt the programmer can call the constructor aand i this example the programmer has called the constructor.

Explicit constructors means that the compiler can't call the constructor. You use explicit constructors when you don't want he compiler calling constructors.
Jun 27 '07 #4
This is not correct:


The example is an explicit conversion. Explicit means that onlt the programmer can call the constructor aand i this example the programmer has called the constructor.

Explicit constructors means that the compiler can't call the constructor. You use explicit constructors when you don't want he compiler calling constructors.

In what scenarios EXPLICIT keyword should be used. ????
Jun 28 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Suppose you have a container with a constructor that has an int argument that is intended to create a container of a specified size:

Expand|Select|Wrap|Line Numbers
  1. MyContainer<int>  obj(100);
  2.  
Let's suppose this contstructor creates room for 100 ints.

Then suppose you have a function like this:

Expand|Select|Wrap|Line Numbers
  1. void fx(MyContainer<int> arg)
  2. {
  3.  
  4. };
  5.  
If you call this function with an int. The MyContainer constructor will be called and an empty container will be passed to the function.

Expand|Select|Wrap|Line Numbers
  1. fx(1234);     //creates a MyContainer with room for 1,234 ints.
  2.  
The above is the implicit call to the constructor. In fact, this is an error. The 1234 should not be used with fx(). So, you make the MyContainer constructor explicit and then you will get a compile error on fx(1234).
Jun 28 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.