Hi all,
I have the following problem ( explanation below code ):
// main.cpp
class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private: // emphasize the following members are private
noncopyable( const noncopyable& );
const noncopyable& operator=( const noncopyable& );
};
class MyClass: noncopyable
{
public:
MyClass( ) { }
};
class IFormatter
{
public:
void func( const MyClass& ) { }
virtual ~IFormatter( ){}
};
int main()
{
MyClass mc;
IFormatter f;
f.func( MyClass( ) ); // doesn't work
f.func( mc ); // works
return 0;
}
This code is accepted by comeau in strict mode and by all MSVC that I
had used 7.0 up to 8.0
..
gcc 4.0.2 complains that the copy constructor of MyClass is private.
Now I wannted to further simplify by removing the noncopyable base and
instead making the copy and assignment private in MyClass.
class MyClass
{
public:
MyClass( ) { }
private:
MyClass( MyClass const& );
MyClass const& operator=( MyClass const& );
};
class IFormatter
{
public:
void func( MyClass const& ) { }
virtual ~IFormatter( ){}
};
int main()
{
MyClass mc;
IFormatter f;
f.func( MyClass( ) );
return 0;
}
surprisingly, now even comeau complains:
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATI ON_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++
"ComeauTest .c", line 35: error: "MyClass::MyCla ss(const MyClass &)",
required for
copy that was eliminated, is inaccessible
f.func( MyClass( ) );
^
Could it be possible that comeau is wrong for one or the other example?
Or did I change something significantly in my simplification?
Could someone try this with a gcc 4.1.x or 3.x.x ?
I appreciate your help and thank you in advance.
Ingo 6 2401
when you call f.func() like this
f.func( MyClass( ) );
compiler has to create tempolary object, that means copy contructor
will be called. You declare copy constructor as a private, so that's
why compiler complains you
Butterfly <Ma**********@g mail.com> wrote: when you call f.func() like this
f.func( MyClass( ) );
compiler has to create tempolary object, that means copy contructor will be called. You declare copy constructor as a private, so that's why compiler complains you
I do not see why the copy ctor would have to be called. Afaik, the
temporary is created using the ctor taking no arguments, and a const
reference to this object is passed. No object copying involved.
regards
--
jb
(reply address in rot13, unscramble first)
nutty wrote: Hi all,
I have the following problem ( explanation below code ):
// main.cpp
class noncopyable { protected: noncopyable() {} ~noncopyable() {} private: // emphasize the following members are private noncopyable( const noncopyable& ); const noncopyable& operator=( const noncopyable& ); };
class MyClass: noncopyable { public: MyClass( ) { } };
class IFormatter { public: void func( const MyClass& ) { } virtual ~IFormatter( ){} }; int main() { MyClass mc; IFormatter f; f.func( MyClass( ) ); // doesn't work f.func( mc ); // works return 0; }
This code is accepted by comeau in strict mode and by all MSVC that I had used 7.0 up to 8.0 .
gcc 4.0.2 complains that the copy constructor of MyClass is private.
Now I wannted to further simplify by removing the noncopyable base and instead making the copy and assignment private in MyClass.
class MyClass { public: MyClass( ) { } private: MyClass( MyClass const& ); MyClass const& operator=( MyClass const& ); };
class IFormatter { public: void func( MyClass const& ) { } virtual ~IFormatter( ){} }; int main() { MyClass mc; IFormatter f; f.func( MyClass( ) ); return 0; }
surprisingly, now even comeau complains:
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATI ON_BETA1 Copyright 1988-2003 Comeau Computing. All rights reserved. MODE:strict errors C++
"ComeauTest .c", line 35: error: "MyClass::MyCla ss(const MyClass &)", required for copy that was eliminated, is inaccessible f.func( MyClass( ) ); ^
Could it be possible that comeau is wrong for one or the other example? Or did I change something significantly in my simplification?
Binding an rvalue to a const reference invokes implementation-defined
behaviour. An implementation can bind directly to the object or copy
construct a temporary object. Comeau obviously does one or the other
depending on the context.
In other words for your code to be portable the copy constructor must
be accessible.
Could someone try this with a gcc 4.1.x or 3.x.x ?
gcc 4.1.1 complains as well in both cases.
Markus Schoder wrote: nutty wrote: Hi all,
I have the following problem ( explanation below code ):
// main.cpp
class noncopyable { protected: noncopyable() {} ~noncopyable() {} private: // emphasize the following members are private noncopyable( const noncopyable& ); const noncopyable& operator=( const noncopyable& ); };
class MyClass: noncopyable { public: MyClass( ) { } };
class IFormatter { public: void func( const MyClass& ) { } virtual ~IFormatter( ){} }; int main() { MyClass mc; IFormatter f; f.func( MyClass( ) ); // doesn't work f.func( mc ); // works return 0; }
This code is accepted by comeau in strict mode and by all MSVC that I had used 7.0 up to 8.0 .
gcc 4.0.2 complains that the copy constructor of MyClass is private.
Now I wannted to further simplify by removing the noncopyable base and instead making the copy and assignment private in MyClass.
class MyClass { public: MyClass( ) { } private: MyClass( MyClass const& ); MyClass const& operator=( MyClass const& ); };
class IFormatter { public: void func( MyClass const& ) { } virtual ~IFormatter( ){} }; int main() { MyClass mc; IFormatter f; f.func( MyClass( ) ); return 0; }
surprisingly, now even comeau complains:
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATI ON_BETA1 Copyright 1988-2003 Comeau Computing. All rights reserved. MODE:strict errors C++
"ComeauTest .c", line 35: error: "MyClass::MyCla ss(const MyClass &)", required for copy that was eliminated, is inaccessible f.func( MyClass( ) ); ^
Could it be possible that comeau is wrong for one or the other example? Or did I change something significantly in my simplification?
Binding an rvalue to a const reference invokes implementation-defined behaviour. An implementation can bind directly to the object or copy construct a temporary object. Comeau obviously does one or the other depending on the context.
In other words for your code to be portable the copy constructor must be accessible.
Actually from the wording in the standard it looks like the copy
constructor must be accessible in all cases even if the copy is not
done which would mean that Comeau gets this wrong.
Markus Schoder schrieb: Actually from the wording in the standard it looks like the copy constructor must be accessible in all cases even if the copy is not done which would mean that Comeau gets this wrong.
Thank you,
your answers, both, finally helped.
So that means for me, either I need to avoid this syntax or I make the
copy constructor available, knowing that at least in release mode no
compiler would ever use it.
Ingo
Markus Schoder wrote: Actually from the wording in the standard it looks like the copy constructor must be accessible in all cases even if the copy is not done which would mean that Comeau gets this wrong.
Indeed, Daveed Vandevoorde admitted the bug in this post: http://groups.google.co.uk/group/com...b0ffb131e94bea
Tom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Torsten Mohr |
last post by:
Hi,
i'd like to pass a reference or a pointer to an object
to a function. The function should then change the
object and the changes should be visible in the calling
function.
In perl this would be something like:
sub func {
|
by: Shea Martin |
last post by:
I have a String class (I know I am re-inventing the wheel, yes I have
heard of boost, and of QString).
My copy constructor does a deep (strcpy) of the char *_buffer member.
I have a member function func(const String ¶m). When an actual
String is passed as the param this is nice and efficient.
I have a constructor which takes a...
|
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...
|
by: bipod.rafique |
last post by:
Hello all,
Even though this topic has been discussed many times, I still need your
help in clearing my confusions. Consider the following code:
class aclass{
public:
aclass(){
|
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
...
| |
by: rKrishna |
last post by:
I was trying to understand the real need for copy constructors. From
literature, the main reason for redfinition of copy constructor in a
program is to allow deep copying; meaning ability to make copies of
classes with members with static or dynamic memory allocation. However
to do this it requires the programmer to override the default copy...
|
by: nagrik |
last post by:
Hello group,
Last week I picked up a thread, which pointed out that if a copy
constructor is created with pointers
instead of reference, there is a danger of it going in infinite
recursion.
My observation:
1. Compiler does not complain.
|
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: sanjay |
last post by:
Hi All,
I have a doubt in understanding the output of the following program
that i executed on my system. I was using DevC++ IDE which uses minGW
based compiler.
----------------------------------------------
#include <iostream>
using namespace std;
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
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.
| | |