class test {
public:
test()
:a(0)
{
}
int get() { return a; }
test(const test& t)
:a(t.get())
{
}
void modify()
{
a++;
}
private:
int a;
};
Intuitively it seems that the parameter to copy constructor should be
const test&
However, if I try to use get() method, my compiler gives the error:
i:\Documents and Settings\Al Cpwn\My Documents\Visua l Studio
Projects\c++\1. cpp(11) : error C2662: 'test::get' : cannot convert
'this' pointer from 'const test' to 'test &'
Can someone please help me understand why? 11 1865
al.c...@gmail.c om wrote: class test { public: test() :a(0) {
} int get() { return a; }
Modify the above line so that get() is a const member function
int get() const { return a; }
In message <11************ **********@j33g 2000cwa.googleg roups.com>, al*****@gmail.c om writes class test { public: test() :a(0) {
} int get() { return a; } test(const test& t) :a(t.get()) { } void modify() { a++; } private: int a; };
Intuitively it seems that the parameter to copy constructor should be const test& However, if I try to use get() method, my compiler gives the error:
i:\Documents and Settings\Al Cpwn\My Documents\Visua l Studio Projects\c++\1 .cpp(11) : error C2662: 'test::get' : cannot convert 'this' pointer from 'const test' to 'test &'
Can someone please help me understand why?
You're trying to call the non-const function get() on the const
reference argument t.
Change it to int get() const { return a; }
--
Richard Herring al*****@gmail.c om wrote: class test { public: test() :a(0) {
} int get() { return a; }
Try:
int get () const { return a; }
test(const test& t) :a(t.get()) { } void modify() { a++; } private: int a; };
Intuitively it seems that the parameter to copy constructor should be const test& However, if I try to use get() method, my compiler gives the error:
Best
Kai-Uwe Bux
I have a question: how does the compiler know which function to call,
is the const keyword sufficient? For primitive types such as int or
double, do we also have const keyword specified for certain operations?
al.cpwn wrote: I have a question: how does the compiler know which function to call, is the const keyword sufficient? For primitive types such as int or double, do we also have const keyword specified for certain operations?
Could you post a code sample of the effect you are asking about?
The guideline is "top-level const is bad karma", but we don't know if you
are asking about top-level const.
--
Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
In message <11************ **********@z34g 2000cwc.googleg roups.com>, al*****@gmail.c om writes
[please quote some context so we know what you're talking about] I have a question: how does the compiler know which function to call, is the const keyword sufficient?
If a class has both const and non-const versions of a member function,
it decides which to call on the basis of whether the object (or the
salient reference or pointer to it) is const or not.
For primitive types such as int or double, do we also have const keyword specified for certain operations?
Primitive types don't have such member functions. Can you give an
example of what you mean?
--
Richard Herring
Richard Herring wrote: In message <11************ **********@z34g 2000cwc.googleg roups.com>, al*****@gmail.c om writes
[please quote some context so we know what you're talking about]
I have a question: how does the compiler know which function to call, is the const keyword sufficient? If a class has both const and non-const versions of a member function, it decides which to call on the basis of whether the object (or the salient reference or pointer to it) is const or not.
For primitive types such as int or double, do we also have const keyword specified for certain operations? Primitive types don't have such member functions. Can you give an example of what you mean?
for example:
int a=0;
a++; //++(int) is non const
const int b=0;
b++; //error
do we get an error because there is no const function named ++(int)? or
do we get an error because the compiler simply knows that ++(int) will
change b somehow.
-- Richard Herring do we get an error because there is no const function named ++(int)? or do we get an error because the compiler simply knows that ++(int) will change b somehow.
Don't you mate aliens because you know it won't bring forth kids, or
do you not mate them because they don't exist?
What's the difference?
-Gernot
Gernot Frisch wrote: do we get an error because there is no const function named ++(int)? or do we get an error because the compiler simply knows that ++(int) will change b somehow.
Don't you mate aliens because you know it won't bring forth kids, or do you not mate them because they don't exist? What's the difference?
thank you for taking the time to share your rhetoric. If you had a
better response that would have been great as well. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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: RonHiler |
last post by:
My copy constructor is crashing my program, and I can't figure out
why. I'll try to make the code listing as short as I can. Here are
the two headers:
class BreakthroughClass
{
public:
BreakthroughClass();
virtual ~BreakthroughClass();
BreakthroughClass(const BreakthroughClass &source);
|
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: sarathy |
last post by:
Hi all,
I have doubt regarding how objects are passed in C++. The
primary problem of passing by value in C++, is that the destructor of
the object passed will be called twice, thus creating possible damage
to the object passed. If that is the case, why isnt the following
program producing the unexpected o/p. I expect the program to print
In B::printValues : 22 33
instead of
|
by: subramanian |
last post by:
Consider the code fragment:
class Test {
public:
Test(const Test &temp);
...
};
....
| |
by: Jeroen |
last post by:
Hi all,
I'm trying to implement a certain class but I have problems regarding
the copy ctor. I'll try to explain this as good as possible and show
what I tried thusfar. Because it's not about a certain code syntax but
more a 'code architecture' thing , I'll use simple example classes
(which are certainly not complete or working...) just to illustrate the
idea (and I may make some mistakes because I'm not that experienced...).
The...
|
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 v);
|
by: JD |
last post by:
Hi,
My associate has written a copy constructor for a class. Now I need to add
an operator = to the class. Is there a way to do it without change her code
(copy constructor) at all? Your help is much appreciated.
JD
|
by: Rahul |
last post by:
Hi Everyone,
It is well known that the input parameter which is passed to the
copy constructor is passed as reference and not as as object. Because
passing an object is as good as making another copy which in itself
needs a copy constructor.
However i was wondering why can't the existing object be passed as
a pointer instead of a reference to the copy constructor which creates
a new object.
|
by: =?ISO-8859-1?Q?Marcel_M=FCller?= |
last post by:
Hi,
is there a way to avoid the automatic copy constructor generation. I do
not want the object to be non-copyable. I simply do not want that
copying is done by the default copy constructor. But there is a
constructor that accepts the base class. This one should be used for
copying.
In fact i have a set of classes with a common abstract base. The
implementations do not have own data members. They only implement
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: 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...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |