Hi,
In the below example, does copy constructor has any role in the
initialization ?
class complex {
private:
double re, im;
public:
complex( double r = 0, double i = 0) :re(r), im(i) {}
complex( const complex& c) :re(c.re), im(c.im) {}
};
int main()
{
complex x = complex(3,2);
return 0;
} 6 1613
Dhirendra Singh wrote: In the below example, does copy constructor has any role in the initialization ?
class complex { private: double re, im; public: complex( double r = 0, double i = 0) :re(r), im(i) {} complex( const complex& c) :re(c.re), im(c.im) {} };
int main() { complex x = complex(3,2); return 0; }
Yes, it does. The language requires it to be accessible _as_if_ it
were used; however, it can be optimized away. Make your copy c-tor
private and try compiling again.
V
--
Please remove capital As from my address when replying by mail
It depends on the compiler you use.
For example belowed compiles without errors on Intel(R) C++ Compiler
v7.1:
#include <iostream>
namespace {
using std::cout;
using std::endl;
class complex {
private:
double re, im;
public:
complex( double r = 0, double i = 0) :re(r), im(i) {
cout << "complex()" << endl;
}
private:
complex( const complex& c) :re(c.re), im(c.im) {
cout << "complex(comple x&)" << endl;
}
};
}
int main()
{
complex x = complex(3,2);
}
It depends on the compiler you use.
For example, the belowed code compiles without any errors on Intel(R)
C++ Compiler
v7.1:
#include <iostream>
namespace {
using std::cout;
using std::endl;
class complex {
private:
double re, im;
public:
complex( double r = 0, double i = 0) :re(r), im(i) {
cout << "complex()" << endl;
}
private:
complex( const complex& c) :re(c.re), im(c.im) {
cout << "complex(comple x&)" << endl;
}
};
}
int main()
{
complex x = complex(3,2);
} ro************* @gmail.com wrote: It depends on the compiler you use. For example belowed compiles without errors on Intel(R) C++ Compiler v7.1:
#include <iostream>
namespace { using std::cout; using std::endl;
class complex { private: double re, im; public: complex( double r = 0, double i = 0) :re(r), im(i) { cout << "complex()" << endl; } private: complex( const complex& c) :re(c.re), im(c.im) { cout << "complex(comple x&)" << endl; }
};
} int main() { complex x = complex(3,2); }
It does? Then it's a bug in the compiler. The code is ill-formed. Try
it with Comeau online, and you'll see what _ought_ to be.
Are you sure you didn't use some kind of "Microsoft compatibility mode"
instead of "normal strict standard C++ rules mode" when you compiled it?
Some compilers are known to let ill-formed code to go through simply
because they want to be popular among sloppy programmers. In my book
it's no good, but folks are often happy that at least well-formed code
compiles. It's up to the users to decide which product is acceptable.
V
--
Please remove capital As from my address when replying by mail
when it is never called then why compiler cries when copy constructor
is declared as private ?
complex x = complex(3,2) is always treated like complex x(3,2)
I am reading the stroutstrap book and did not find anything mentioned
about it.
Dhirendra Singh wrote: when it is never called then why compiler cries when copy constructor is declared as private ?
Because the Standard requires it to.
complex x = complex(3,2) is always treated like complex x(3,2) I am reading the stroutstrap book and did not find anything mentioned about it.
The statement
complex x = complex(3,2);
is *semantically* equivalent to
complex _some_temporary _value(3,2);
complex x(_some_tempora ry_value);
The Standard _allows_ to forgo creation of the temporary and generate
code that would be the same as writing
complex x(3,2);
*HOWEVER* the copy constructor _shall_ be available *as if* it is used.
V
--
Please remove capital As from my address when replying by mail This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: tracy |
last post by:
hi,
just wonder, can we copy a role then add some new priviledges to the
new role.
hm.. i means, example; now i have a role named role_a. Then I copy
role_a to create role_b. After I created role_b from role_a, i would
like to revoke some privileges in role_b without affecting role_a. Can
I and how to write the script?
regards,
tracy
|
by: gudia |
last post by:
How would I, using a sql script, copy permissions assigned to a user
or a role in one or more databases to another user or a role in their
respective databases?
Help appreciated
|
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 kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn't
mean that they couldn't be copied from another object of the same kind when...
|
by: craig |
last post by:
I am working on my first .NET development project that involves custom
role-based security per the project requirements. This lead to a general
design issue this week that really caused us some concern. I have described
the situation below because we are very curious to see what other, more
experienced, developers might suggest. The specific classes and fields are
used just to illustrate the concepts.
Our application uses role-based...
|
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 pointers) and for objects types call
their default constructor.
Any others points i should know?
| |
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, we do not need to use the
copy-constructor. So depending on the above reasoning I can avoid call
by value and return by value for class objects, this bypasses the
problem or it seems to me like that.
Could any one give me some simple examples...
|
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)
{}
|
by: dragoncoder |
last post by:
Hi all,
I am trying to understand the auto_ptr_ref role in the implementation
of auto_ptr<>. I read the information on net but still not 100% sure of
it. My plan is as follows.
1. To see the behaviour of std::auto_ptr.
2. To implement a pkt::auto_ptr without auto_ptr_ref.
3. Check out the limitations of pkt::auto_ptr as compared to
std::auto_ptr (using steps 1 and 2).
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |