constructor

In what order are global constructors called

為{幸葍}努か 提交于 2020-01-04 05:21:05
问题 In what order get the constructors of global object in C++ called? This question arises in the context of a memory pool that manages the memory needs for some consumers. I was presented with a rather large source code which defines at global namespace some consumers just using heap functions. A memory pool should be added without changing the namespace of the consumers. So, I added a pool class and a definition at global namespace and modified each consumer class to get memory from the

In C++ are constructors called before or after object creation?

别说谁变了你拦得住时间么 提交于 2020-01-04 04:07:49
问题 I found some answers for this question regarding java but nothing specifically regarding c++. So I've read in Java the object is first created and then the constructor is called. I was wondering if this was the same process for c++? Also, if this is the case, then what's the point of having a default constructor at all? Is it for inheritance purposes? 回答1: "Object creation" means different things in different languages. But in C++ the most salient question is "when does the object lifetime

In C++ are constructors called before or after object creation?

别等时光非礼了梦想. 提交于 2020-01-04 04:07:04
问题 I found some answers for this question regarding java but nothing specifically regarding c++. So I've read in Java the object is first created and then the constructor is called. I was wondering if this was the same process for c++? Also, if this is the case, then what's the point of having a default constructor at all? Is it for inheritance purposes? 回答1: "Object creation" means different things in different languages. But in C++ the most salient question is "when does the object lifetime

wrong argument conversion preferred when calling function

本小妞迷上赌 提交于 2020-01-04 04:00:46
问题 I'm writing a program under MS Visual C++ 6.0 (yes, I know it's ancient, no there's nothing I can do to upgrade). I'm seeing some behavior that I think is really weird. I have a class with two constructors defined like this: class MyClass { public: explicit MyClass(bool bAbsolute = true, bool bLocation = false) : m_bAbsolute(bAbsolute), m_bLocation(bLocation) { ; } MyClass(const RWCString& strPath, bool bLocation = false); private: bool m_bAbsolute; bool m_bLocation; }; When I instantiate an

C++: why constructor “A(A a) {}” is illegal? [duplicate]

こ雲淡風輕ζ 提交于 2020-01-04 03:58:48
问题 This question already has answers here : Why should the copy constructor accept its parameter by reference in C++? (8 answers) Closed 4 years ago . I met a quiz saying that the code below is ill-formed because "It is illegal to have a constructor whose first and only non-default argument is a value parameter for the class type." I couldn't understand that. Why things like A(A a) : val (a.val) {} is ruled as illegal? why such a line in the standard? Is it because this will lead to ambiguity

C++ Call base constructor with enum

好久不见. 提交于 2020-01-04 03:15:10
问题 Is it possible to pass a value and a constant enum to the base constructor of a class? For example: enum CarBrand { Volkswagen, Ferrari, Bugatti }; class Car { public: Car(int horsePower, CarBrand brand) { this->horsePower = horsePower; this->brand = brand; } ~Car() { } private: int horsePower; CarBrand brand; }; class FerrariCar : public Car { public: // Why am I not allowed to do this ..? FerrariCar(int horsePower) : Car(horsePower, CarBrand.Ferrari) { } ~FerrariCar(); }; Because I'm

Joda Time classes don't have any constructors… why? And what am I doing wrong?

▼魔方 西西 提交于 2020-01-04 03:00:13
问题 Apparently Scala on Eclipse tries to convince me that DateTime , Period , DateMidnight and many other classes in Joda Time don't have any constructors Which is odd considering the fact that the documentation for them shows a constructor and a number of methods The only things I have access to are the static methods such as DateTime.now() and DateTime.parse() Note that I have added the joda-time-2.2.jar , joda-time-2.2-sources.jar , and joda-time-2.2-javadoc.jar files to the java build path of

C++ Array Initialization in Function Call or Constructor Call

冷暖自知 提交于 2020-01-03 16:41:09
问题 This question is related to the post here. Is it possible to initialize an array in a function call or constructor call? For example, class foo's constructor wants an array of size 3, so I want to call foo( { 0, 0, 0 } ). I've tried this, and it does not work. I'd like to be able to initialize objects of type foo in other objects' constructor initialization lists, or initialize foo's without first creating a separate array. Is this possible? 回答1: Not in the current standard. It will be

Conversion operator in direct-initialization

久未见 提交于 2020-01-03 13:04:57
问题 The C++14 standard (N4296) says in 8.5/17.6.1 If the initialization is direct-initialization [...], constructors are considered. The applicable constructors are enumerated, and the best one is chosen through overload resolution. [...] If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed. Therefore in direct-initialization, only constructors are considered - conversion functions are ignored. In the following code there is no applicable

Getter and Setter conventions in go lang

帅比萌擦擦* 提交于 2020-01-03 10:59:55
问题 Case A Not following the Getter & Setter convention human/human.go package human type Human interface { GetName() string SetName(name string) } type Person struct { Name string } func (p Person) GetName() string { return p.Name } func (p *Person) SetName(name string) { p.Name = name } main/main.go package main func main() { john := Person{Name:"john"} // Uppercase Fields are visible fmt.Println(john) } Case B Following getter and setter convention package human type Human interface { Name()