constructor

Error: No instance of constructor matches the argument list

前提是你 提交于 2020-02-03 10:05:08
问题 As part of an assignment we have been asked to create a Vector3D class which uses memory allocated on the Heap. I have a Vector3DHeap class with the following constructor. Vector3DHeap::Vector3DHeap(float& x, float& y, float& z) { this->x = &x; this->y = &y; this->z = &z; } If I want to get a unit vector, I was expecting the be able to do the following. This gives the error message "No instance of constructor matches the argument list, argument types are (float, float, float). Vector3DHeap*

default values from not defined constructor

牧云@^-^@ 提交于 2020-02-03 09:47:45
问题 Bjarne wrote:- For a type T, T() is the notation for the default value , as defined by the default constructor . What happen when we don't declare default constructor ? For example using namespace std; class date{ int n,m; public: int day(){return n;} int month(){return m;} };//no default constructor int main() { date any =date(); cout<<any.month()<<endl; cout<<any.day()<<endl; return 0; } Output of this program is 0 and 0 every time i run my program. I haven't declare any default constructor

Is it allowed to call a non-static member function in a default member initializer?

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-02 11:27:12
问题 Consider this class: #include <iostream> struct foo { int a = 42; int b = bar(); int bar() { return a; } }; int main(){ foo f; std::cout << f.a << " " << f.b; } It prints the expected 42 42 . Is it allowed by the standard to call a member function in a default member initializer? The following I would expect to be undefined: struct broken { int a = bar(); int b = 42; int bar() { return b; } }; Unfortunately it does compile without warnings. 回答1: As you found, this is legal, but brittle and

C++ constructor template specialization

旧巷老猫 提交于 2020-02-02 02:59:39
问题 I'm trying to create a specialized constructor for std::string arguments, but the other one is always used when I call it with a string argument. struct Literal : Expression { template <typename V> Literal(V val) { value = val; } }; template <> Literal::Literal(std::string const& val) { value = val.c_str(); } It doesn't matter if both are defined inside the class, both outside the class, or like in the posted example only the specialization is defined outside the class: When called with std:

Why are you allowed to call one constructor from another?

我是研究僧i 提交于 2020-01-31 07:12:36
问题 I was looking at other questions on SO, but I didn't really see an explanation for my question. I read that calling a constructor from another constructor (using the this keyword) was valid , but I didn't understand why it was valid. Previously, I thought that only one constructor could act per object. Constructor chaining seems to break this logic, in that while calling one constructor, it runs another in conjunction to the original, targeted constructor. Why does constructor chaining work?

Why are you allowed to call one constructor from another?

江枫思渺然 提交于 2020-01-31 07:11:38
问题 I was looking at other questions on SO, but I didn't really see an explanation for my question. I read that calling a constructor from another constructor (using the this keyword) was valid , but I didn't understand why it was valid. Previously, I thought that only one constructor could act per object. Constructor chaining seems to break this logic, in that while calling one constructor, it runs another in conjunction to the original, targeted constructor. Why does constructor chaining work?

How to initialize a unique_ptr

别等时光非礼了梦想. 提交于 2020-01-31 06:16:25
问题 I'm trying to add a lazy-initialization function to my class. I'm not very proficient with C++. Can someone please tell me how I achieve it. My class has a private member defined as: std::unique_ptr<Animal> animal; Here's the original constructor that takes one parameter: MyClass::MyClass(string file) : animal(new Animal(file)) {} I just added a parameter-less constructor and an Init() function. Here's the Init function I just added: void MyClass::Init(string file) { this->animal = ???; }

How to initialize a unique_ptr

天大地大妈咪最大 提交于 2020-01-31 06:16:13
问题 I'm trying to add a lazy-initialization function to my class. I'm not very proficient with C++. Can someone please tell me how I achieve it. My class has a private member defined as: std::unique_ptr<Animal> animal; Here's the original constructor that takes one parameter: MyClass::MyClass(string file) : animal(new Animal(file)) {} I just added a parameter-less constructor and an Init() function. Here's the Init function I just added: void MyClass::Init(string file) { this->animal = ???; }

When does the vptr (pointing to vtable) get initialized for a polymorphic class?

风流意气都作罢 提交于 2020-01-31 02:21:30
问题 This is not about "When VTABLE is created?". Rather, when the VPTR should be initialized? Is it at the beginning/end of the constructor or before/after the constructor? A::A () : i(0), j(0) -->> here ? { -->> here ? //... -->> here ? } 回答1: The machinery for virtual calls (usually a v-table, but doesn't need to be) is set up during the ctor-initializer , after construction of base subobjects and before construction of members. Section [class.base.init] decrees: Member functions (including

Understanding Factory constructor code example - Dart

北城以北 提交于 2020-01-30 14:09:05
问题 I have some niggling questions about factory constructors example mentioned here (https://www.dartlang.org/guides/language/language-tour#factory-constructors). I am aware of only three types of constructors on a basic level - default, named and parameterised. Why should I use factory at all for this example? Is that a named constructor which is being used? and why? 回答1: tl;dr Use a factory in situations where you don't necessarily want to return a new instance of the class itself. Use cases: