Questions tagged [copy-initialization]

Initializes an object from another object

52 questions
2
votes
1 answer

C++ object construction -- direct initialization versus using the '=' operator, are they equivalent?

In C++, are these two styles of initializing a class-object functionally equivalent, or are there some situations where they might have differing semantics and generate different code? SomeClass foo(1,2,3); vs auto foo = SomeClass(1,2,3);
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
2
votes
1 answer

Why does the class reference constructor overload get called from an rvalue when there's an rvalue constructor available?

This code #include struct A { A(int i) {std::cout << "int received\n";} A(A& a) {std::cout << "ref received\n";} }; int main() { int j = 5; A a = j; } unexpectedly throws the following compiler error: error: invalid…
GT 77
  • 448
  • 5
  • 12
2
votes
1 answer

Difference copy-initialization and direct-initialization for primitive (scalar) types

as far as I understand in C++ is an initialization in the form T x = a; called copy-initialization and an initialization in the form T x(a); or T x{a}; called direct-initialization. (T...Type, x...variable name, a...expression) For class types…
knowledge
  • 941
  • 1
  • 11
  • 26
2
votes
2 answers

no matching function in template class

I get no matching member function error when i try to compile this code on my mingw32 compiler #include using std::cout; template class Pattern { public: Pattern(): element(){ cout<< "default c-tor"; } …
Brian
  • 21
  • 1
2
votes
1 answer

Do implicit class-type conversions use the copy-constructor?

The following quote from my C++ book: When we use direct initialization, we are asking the compiler to use ordinary function matching to select the constructor that best matches the arguments we provide. When we use copy initialization, we are…
2
votes
2 answers

Explicit and non-explicit constructors

class Test { public: Test(int i) { cout<<"constructor called\n";} Test(const Test& t) { cout<<" copy constructor called\n";} }; class Test1 { public: Test1(int i) { cout<<"constructor…
1
vote
1 answer

Why this copy-initialization (with two implicit conversions) does not work in GCC/Clang?

Please consider the following code: #include class cstring { public: cstring(const char* str) : begin_(str), end_(str ? str + strlen(str) : 0) { } size_t size() const { return (size_t)(end_ - begin_); } …
amanjiang
  • 1,213
  • 14
  • 33
1
vote
1 answer

C++ Stringstream initializations

I have two code snippets: This does not compile: std::string reverseSentence(std::string sentence) { std::stringstream stream = sentence; } This does: std::stringstream stream (sentence); It's my understanding that T foo = expr is T…
1
vote
2 answers

Difference between = and {} syntaxes for initializing a variable in C++

I have read quite a few C++ codes, and I have come across two methods of initialising a variable. Method 1: int score = 0; Method 2: int score {}; I know that int score {}; will initialise the score to 0, and so will int score = 0; What is the…
1
vote
1 answer

To Clarify my insights to copy initialization and direct initialization

Define a class as follows: class A { public: A(): s("") {} //default constructor A(const char* pStr): s(pStr) {} //constructor with parameter A(const A& a) : s(a.s) {} //copy constructor …
Finley
  • 795
  • 1
  • 8
  • 26
1
vote
0 answers

Why do we differ between copy and direct initialization?

I have following questions and I hope you can help me. Code: A a; //A(void) is called A b(a); //A(const A&) is called: direct-initialization A c = a; //A(const A&) is called: copy-initialization Why are we differ between these initializations? 1)…
1
vote
2 answers

C++ copy initialization + implicit constructor call = fail

This code: class foo { int x; public: foo(int x) : x(x) { } int get() const { return x; } //... }; class bar { int x; public: bar(const foo& x) : x(x.get()) { } int get() const { return x; } bar& operator =(const…
0
votes
1 answer

In C++, under the case `T object(other);`, is it direct initialization or copy initialization?

Assume I have this code T object(other); It is direct initialization or copy initialization? Based on the rule of direct initialization : T object ( arg ); initialization with a nonempty parenthesized list of expressions It is direct…
0
votes
1 answer

Why copy intialisation is used instead of direct initialisation when passing argument to function parameter by value

I am learning C++ using the resources listed here. In particular, I came to know that copy initialization is used when passing arguments to function parameters(by value) etc. For example, from decl.init.general#14: The initialization that occurs in…
0
votes
2 answers

Ambiguous Class Template Conversion

How would one add a template constructor to the class so that copy initialization from complex to complex is performed explicitly and without ambiguity? Is there a solution that is compiler and C++ version/standard agnostic? Is there an approach…