Disabling Copy Constructor

May 18, 2012 at 8:31pm
I'm trying to learn about C++11 disabling constructors, so i downloaded and installed VS11 Ultimate Beta.
When i try to compile my little code i got errors. This is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <iostream>
class MyClass
{
	friend std::ostream& operator<<(std::ostream &, const MyClass &);
public:
	MyClass(int number=0,std::string string="");
	~MyClass();

	//disable copy
	MyClass(const MyClass &copyClass)=delete;
	MyClass& operator=(const MyClass &rightClass)=delete;

private:
	int aNumber;
	std::string aString;
};

I got these errors:

1>..\myclass.h(12): error C2059: syntax error : ';'
1>..\myclass.h(12): error C2238: unexpected token(s) preceding ';'
1>..\myclass.h(13): error C2059: syntax error : ';'
1>..\myclass.h(13): error C2238: unexpected token(s) preceding ';'

It will link me to:
1
2
MyClass(const MyClass &copyClass)=delete;
MyClass& operator=(const MyClass &rightClass)=delete;

What is wrong?
May 18, 2012 at 8:39pm
Not yet supported.
VS11 ccompliance with C++11 list of features is here:

http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx
Last edited on May 18, 2012 at 8:40pm
May 18, 2012 at 8:43pm
Oh! I see... so i've to w8 :S Thx for fast reply!
May 18, 2012 at 8:44pm
No version of Visual Studio supports this feature. For now, all you can do is declare those functions in the private section and never define them.
May 18, 2012 at 9:11pm
I see.
There is a way to store inside a vector a normal class and not its copy?
Some1, some month ago, said me that it's possible to do disabling copy constructor and assign op.
I'm tring something like this:
1
2
3
4
5
6
7
int main()
{
	std::vector<MyClass> vect;
	vect.push_back(MyClass(23,"Prova"));
	system ("PAUSE");
	return EXIT_SUCCESS;
}

But it will create a class then vect will store a copy of MyClass.
May 18, 2012 at 9:31pm
if MyClass has a move constructor, it will create an object and then 'move' it into the vector, bypassing the copy constructor.
May 18, 2012 at 9:37pm
@TheEnigmist
If your compiler suppors emplace for vectors, it is

vect.emplace_back(23,"Prova");
May 18, 2012 at 10:09pm
MS VC++ is a very bad compiler! Apart from that it does not support many features of the C++ 11, it has many bugs that are not being fixed during many years. So if there is a possibility to use another compiler then use it!
An example of the very old bug of the compiler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"

class A
{
public:
	A() {}
private:
	A( const A & );
};

class B : public A
{
};

class C
{
	A a;
};

int _tmain(int argc, _TCHAR* argv[])
{
	B b = B();
	C c = C();

	return 0;
}


There is a very bad management of the compiler project in Microsoft.
Last edited on May 18, 2012 at 10:48pm
May 18, 2012 at 10:15pm
@Vlad
All ppl around internet say taht MS VS is best compiler (or debugger?). What do you suggest as C++ compiler?
May 18, 2012 at 10:20pm
The problem is that Microsoft keeps the monopoly due to big money. It is not the question of the best compiler. It is the question of the monopoly.
Last edited on May 18, 2012 at 10:21pm
May 18, 2012 at 10:27pm
I see. So we can do nothing about this. I know only Visual Studio and Eclipse as compiler :S
May 18, 2012 at 10:33pm
You can use also for example GCC 4.7
May 18, 2012 at 10:40pm
That is a pretty heinous bug. It might lead you to believe that:

1
2
3
4
B b ;
C c ;
B b2(b) ;
C c2(c) ;


would compile, but, of course, it doesn't. Who knows why that one particular bug hasn't been top priority? It's clearly disabling. I don't know how people manage to code productively with a bug like this.

Any chance we can skip the proselytizing?
Last edited on May 18, 2012 at 10:42pm
May 18, 2012 at 10:42pm
It is funny that this bug is known during many years.:)
May 18, 2012 at 10:44pm
Gcc support deleting copy constructors :P
It's time to change compiler!
IT is possible to use VS and GCC? I think VS's debugging is the best on net! Am i wrong?
Last edited on May 18, 2012 at 10:47pm
Topic archived. No new replies allowed.