Questions tagged [copy-initialization]

Initializes an object from another object

52 questions
4
votes
2 answers

copy initialization : why move or copy constructor was not called even if copy-elision is turned off?

My question is different because I may "know" copy-elision. I am learning copy initialization. However,the following code confused me because I have already turned off the copy-elision using -fno-elide-contructors -O0 option. #include…
4
votes
1 answer

Why is a conversion operator not called when using initialization syntax, and why does the clang error message seem wrong?

I have the following code, which constructs one object t2 using a explicit conversion constructor, which performs an implicit conversion of t1. This is expected, and is described in The C++ Programming Language, in section 11.4.1 of the 3rd…
peter
  • 43
  • 2
  • 4
4
votes
1 answer

Why can't I copy-initialize a stringstream from a string?

The following code fails in GCC, Clang and Visual Studio: #include #include int main() { std::string s = "hello"; // ok, copy-initialization std::stringstream ss1(s); // ok, direct-initialization std::stringstream ss2…
Nikolai
  • 3,053
  • 3
  • 24
  • 33
3
votes
1 answer

Why here template Vector3 cannot convert to Vector3?

It seems quite weird. Here you can see the error message is that a convert happens between one type and it fails. If I remove the explicit modifier from Vector3's copy constructor it is fine, no error. Could someone explain why? I'm…
3
votes
1 answer

Is copy initialization identical to copy initialization of a copy?

The following two code segments presents the task of initializing variable b as a copy of a. The first code segment initializes the variable using copy initialization (initialization using =). Assume class Apple is simply defined as an empty…
3
votes
1 answer

Issues about the copy-initialization of reference type

#include struct Data{}; struct Test{ Test() = default; Test(Data){} }; int main(){ Data d; Test const& rf = d; } Consider the above code, The standard says: Otherwise: 5.2.2.1 If T1 or T2 is a class type and T1 is not…
xmh0511
  • 7,010
  • 1
  • 9
  • 36
3
votes
1 answer

Confused about c++ constructor deletion

This is my class class A { public: A(int); A(A const&) = delete; A& operator=(const A& a) = delete; // etc. }; In another file I can call the constructor like this: auto foo = A(123); That resolves to the copy constructor and not the…
Allie
  • 57
  • 2
3
votes
2 answers

What actually happens when copy initializing in C++?

Consider the following code: #include using namespace std; class A{ public: A()=default; A(int a){cout<<"A(int) called"<
sakugawa
  • 117
  • 5
3
votes
2 answers

removing the const in copy constructor

This is what I did originally. class A { public: A() { std::cout << "\ndefault constructor"; } A(const A&) { std::cout << "\ncopy constructor"; } A(int) { std::cout << "\nconversion constructor"; } }; A a0; …
Dick Chow
  • 31
  • 2
3
votes
2 answers

conversion operator bypasses constructor of the derived type and skips to copy-initialization of the base type?

There is something wrong with how the conversion operator behaves in the following code: struct A{ A(){}; A(const A &) = delete; }; struct B : A{ B(const B & x){}; B(int x){}; }; struct C{ operator B() { return B(1); …
3
votes
2 answers

C++(11): When to use direct or copy initialization if both are perfectly fine

Before the shouts for duplicate begin: I am aware that the following question (and some others) are quite related to this one: Is there a difference in C++ between copy initialization and direct initialization? The answers in this question perfectly…
gexicide
  • 38,535
  • 21
  • 92
  • 152
3
votes
3 answers

Difference between $a=&$b , $a = $b and $a= clone $b in PHP OOP

What is the difference between $a = &$b, $a = $b and $b = clone $a in PHP OOP? $a is an instance of a class.
Anonim Wd
  • 65
  • 2
  • 9
2
votes
1 answer

Direct initialization != copy initialization when converting from different type?

I always thought direct initialization and copy initialization for types T that do not match the class type are absolutely equal. Yet I seem to be mistaken. The following code doesn't compile if I copy initialize (using = ) and only compiles when I…
glades
  • 3,778
  • 1
  • 12
  • 34
2
votes
1 answer

Is copy constructor still involved in copy initialization?

Consider the following code: class Y {}; class X { public: X() { } X(const Y&) { } explicit X(const X&) { } }; X f() { return X(); } int main() { Y y; X x = y; f(); } The code gives an error (cannot convert from 'X' to…
CPPL
  • 726
  • 1
  • 10
2
votes
1 answer

Implicit initialization by argument deduction for a templated class array

I have an item class, thats really a wrapper around an int. I have a templated node class. I pass item class to it as template parameter. I want to create an array of the templated node class, but compiler does not like it. When I create a single…