constructor

Inherited constructor, compiles in clang++3.9, fails in g++ 7

不羁的心 提交于 2019-12-10 17:17:17
问题 This code snippet struct Base{}; struct Derived: Base{ using Base::Base; }; int main() { Base b; Derived d{b}; } compiles fine on clang++3.9, however it fails on all gcc's (including 7) and clangs with versions smaller than 3.9 with the error msg error: no matching function for call to 'Derived::Derived() Derived d{b}'. Is the code above standard compliant or not? PS: if I comment out the using Base::Base line, the code does not compile anymore on clang-3.9. 回答1: This is CWG 2356. It appears

Which design to chose for complex object initialization?

☆樱花仙子☆ 提交于 2019-12-10 17:09:50
问题 Lets say I have a class encapsulating one (or multiple) member(s) which must in some way be initialized and there is no reasonable way to use the class without it (so I don't want to make it optional). Is it then better to have initialization run in its constructor like so: class MyClass { MyClass() { if(!obj.initialize() throw ...; } private: MyObject obj; } or would you suggest the following design: class MyClass { MyClass() { } bool initialize() { return obj.initialize(); } private:

Are uninitialised serial port attributes given default values as they are when using the default constructor?

拥有回忆 提交于 2019-12-10 16:49:08
问题 If creating a serial port with the default constructor or parameterless constructor, the port is given default values. From the documentation: // Create a new SerialPort object with default settings. _serialPort = new SerialPort(); This constructor uses default property values when none are specified. For example, the DataBits property defaults to 8, the Parity property defaults to the None enumeration value, the StopBits property defaults to 1, and a default port name of COM1. There are

Why can't access constructor in body of another constructor?

不羁岁月 提交于 2019-12-10 16:39:31
问题 I know that i can access constructor on another constructor like below but is there a way to access it in body of constructor ? public Rectangle(int size) : this(size, size) { Console.WriteLine("Square Constructor Called"); //this(size,size); i want to access like this } 回答1: You can't do that. The full justification is here, but in summary: In short, achieving the desired construction control flow is easy to do without adding the [the ability to call base constructors in arbitrary locations]

PhpStorm: get code inspection warnings for “Methods with the same name as their class will not be constructors”

纵饮孤独 提交于 2019-12-10 16:31:29
问题 Okay I've finally switched to PHP7. My code is a bit old and will be refurbished. Some of the problems are: class MagicClass function MagicClass(){ //etc } Which gives an deprecation warning during execution: Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; MagicClass has a deprecated constructor in This is good: class MagicClass function __construct(){ //etc } How can I get PhpStorm code inspection to warn me for such errors in my

Is it possible in Java to initialise a final data member based on constructor call?

故事扮演 提交于 2019-12-10 16:28:37
问题 Is it possible to make a modification as specified in the class below, and initialize a member for existing callers to some default value, say null ? Member is required to be private final as persistence requirement. // initial version of the class public class A { A() { // do some work here } } // the following modification required adding additional constructor to the class with **member** data member. public class A { private final String member; A(String member) { this(); this.member =

Java no suitable constructor found

ぐ巨炮叔叔 提交于 2019-12-10 16:16:06
问题 For my assignment I have created a class called Agency which will store data about talent agencies and i have created the required attributes. I've also been given a data file which i will need to read from. But first i am to test my class. I can use the getter and setters fine to display my desired output but now I want to create new instance of object Agency and add the details their but i get an error message saying no suitable constructor found. I have matched the settings to my

VB.Net calling New without assigning value

白昼怎懂夜的黑 提交于 2019-12-10 15:57:55
问题 In C# I can do this: new SomeObjectType("abc", 10); In other words, I can call new without assigning the created instance to any variable. However, in VB.Net it seems I cannot do the same thing. New SomeObjectType("abc", 10) ' syntax error Is there a way to do this in VB.Net ? 回答1: See the answers to this other SO Answer So this should work: With New SomeObjectType("abc", 10) End With 回答2: The following works on the Mono VB compiler ( vbnc , version 0.0.0.5914, Mono 2.4.2 - r): Call New

Uninitialized reference member in C++

*爱你&永不变心* 提交于 2019-12-10 15:54:07
问题 I have made class in C++ and I wanted it to have a field Osoba& but I get an weird error: class Rachunek{ public: Osoba& wlasciciel; double stan_konta; Rachunek(Osoba* wlasciciel, double stan_konta){ //Uninitialized reference member this->wlasciciel = wlasciciel; this->stan_konta = stan_konta; } }; 回答1: Use initializing list like this: (Best approach) class Rachunek{ public: Osoba& wlasciciel; double stan_konta; Rachunek(Osoba* wlasciciel, double stan_konta): wlasciciel(*wlasciciel) , stan

Why can an initializer-list not be used as an argument?

↘锁芯ラ 提交于 2019-12-10 15:34:54
问题 struct X { int a; int b; }; int f(X x) { return x.a + x.b; } int main() { int n = f({1, 2}); } Visual Studio 2012 (Nov CTP) reports: error C2664: 'int f(const X &)' : cannot convert parameter 1 from 'initializer-list' to 'X' Reason: cannot convert from 'initializer-list' to 'X' Only an initializer-list with zero or one elements can be converted to this type Build FAILED. 回答1: Visual Studio 2012 (Nov CTP) reports: It's not even a beta compiler. It's supposed to work. I'd link to your code