473,790 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

call copy constructor in "=" operator

Dear All,

I am new to c++ and when write below code that try to call copy
constructor
in "=" operator overloading, it can not compile. Can anyone point out
for
me the reason? thanks !!

class AA
{
public:

AA( AA & obj)
{
}

AA & operator=( AA & obj)
{
AA(obj);
return *this;
}

};

int main()
{

return 0;
}

pp.cc: In method `class AA & AA::operator =(AA &)':
pp.cc:11: declaration of `obj' shadows a parameter
pp.cc:11: no matching function for call to `AA::AA ()'
pp.cc:6: candidates are: AA::AA(AA &)

Feb 13 '06
11 2705
dc wrote:
This is quite strange

Suppose AA has a constructor with arg int.
int inst;
........
AA (inst);//Treats inst as a variable of AA.
AA(10);//OK


The rule is: If it could compile OK as a declaration, then it is;
otherwise it isn't. :) Here's another example:

typedef int x;
class A { };

void foo()
{
A b(x); // declares function b taking int as parameter
// and returning an A (!)

A (x); // declares local variable named 'x'
// (hides the typedef)
A a(x); // creates object 'a' , passing local variable
// 'x' to its copy constructor

}

Feb 14 '06 #11
* Old Wolf:
dc wrote:
This is quite strange

Suppose AA has a constructor with arg int.
int inst;
........
AA (inst);//Treats inst as a variable of AA.
AA(10);//OK


The rule is: If it could compile OK as a declaration, then it is;
otherwise it isn't. :) Here's another example:

typedef int x;
class A { };

void foo()
{
A b(x); // declares function b taking int as parameter
// and returning an A (!)

A (x); // declares local variable named 'x'
// (hides the typedef)
A a(x); // creates object 'a' , passing local variable
// 'x' to its copy constructor

}


He he... :-) :-) :-)

The description "failed experiment" is very apt.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 14 '06 #12

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

Similar topics

3
46968
by: ¤ Alias | last post by:
I have a function named getID3info (lvwDiscInfo.SelectedItem). What is the difference between getID3info (lvwDiscInfo.SelectedItem) and Call getID3info(lvwDiscInfo.SelectedItem) ?
2
2246
by: Nick Jacobson | last post by:
This question is with regard to the * operator as used for sequence concatenation. There's the well-known gotcha: a = ] b = a*3 b = 4 print b
9
3721
by: Pierre Senellart | last post by:
The C++ standard states (26.3.2.1), about std::valarray constructors: > explicit valarray(size_t); > > The array created by this constructor has a length equal to the value of > the argument. The elements of the array are constructed using the default > constructor for the instantiating type T. Does that mean that, on built-in types (e.g. int), "default initialization" (as described in 8.5) is performed?
7
5086
by: Vaca Louca | last post by:
Hello, My setup: Debian sarge on dual Pentium 4. g++ 3.3.5-3. (the other system is Windows XP with MS Visual Studio .NET 2003) I have an auto_array<T> template (based on a template taken from the Corona project hosted at SourceForge) which basically wants to implement std::auto_ptr<T> semantics for an array.
2
2576
by: srktnc | last post by:
When I run the program, I get a Debug Error saying "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information." I put a cout statement (//cout << "len of cPtr: " << _len << endl; ) in my constructor and see that _len is 3435973837 though my character pointer has only a few characters. Then I get the usual message as state above.
19
3581
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and one web presentation tier (Foo.WebFiles). The data tier shall only be accessible thru the business tier so I do NOT want a reference to the data tier in the presentation tier. In the business tier I have a class with the name CategoryItem that...
1
7718
by: RajS | last post by:
Hi All, I wanted increase time out on odbc connection, I have code like this this.odbcConnection1.set_ConnectionTimeout(1000); I got the following error "cannot explicitly call operator or accessor" How can I increase the time out? Thanks,
4
1897
by: Dan Stromberg | last post by:
Hi folks. I'm working on building some software, some of which is written in C++, for a researcher here at the University. I have an extensive background in C and python, but I haven't done much with C++ - I kind of mostly abandoned C++ some time ago, when I coded a project in C++, and some of my coworkers refused to use it -because- it was in C++.
3
1976
by: Jess | last post by:
Hello, The C++ reference says the return result of "copy" is an output iterator. I'm wondering how I can assign the returned iterator to some other iterator. I tried int main(){ string s("abcdefg"); vector<charv; vector<char>::iterator k = copy(s.begin(),s.end(),back_inserter(v));
1
3782
by: jr.freester | last post by:
I've written a Matrix container class and overloaded the function call operator to return values at a specified index. Below is the member function double operator()(int a , int b) { if((a < 1) || (b < 1) || ((a + 1) this->row) || ((b + 1) this- { cerr << "Invalid index for Matrix" <<endl; }
0
9666
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, 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...
0
9512
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,...
0
10413
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, 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...
0
9021
agi2029
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...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
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();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
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
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.