constructor

Is std::cout guaranteed to be initialized?

断了今生、忘了曾经 提交于 2019-12-10 13:45:56
问题 What I know about C++ is that the order of the constructions (and destructions) of global instances should not be assumed. While I'm writing code with a global instance which uses std::cout in the constructor & destructor, I got a question. std::cout is also a global instance of iostream. Is std::cout guaranteed to be initialized before any other global instances? I wrote a simple test code and it works perfectly, but still I don't know why. #include <iostream> struct test { test() { std:

What is the meaning of leading underscores in a C++ constructor?

ε祈祈猫儿з 提交于 2019-12-10 13:41:26
问题 OK I am not a very experienced C++ programmer, but I was wondering what is the significance of the underscores in the arguments of the following constructor? class floatCoords { public: floatCoords(float _x, float _y, float _width, float _height) : x(_x), y(_y), width(_width), height(_height) { } float x, y, width, height; ... 回答1: It's just a convenient naming convention, it means nothing to the language. Just be sure you don't follow it with an upper-case letter: What does double underscore

Is this trick, to make calling shared_from_this() in the constructor 'just work', dangerous?

痞子三分冷 提交于 2019-12-10 13:33:00
问题 Question for the C++ experts. We all know that calling shared_from_this() in the class constructor will result in a bad_weak_ptr exception, because no shared_ptr to the instance has been created yet. As a work-around for that, I came up with this trick: class MyClass : public std::enable_shared_from_this<MyClass> { public: MyClass() {} MyClass( const MyClass& parent ) { // Create a temporary shared pointer with a null-deleter // to prevent the instance from being destroyed when it // goes out

Initializing class without default constructor

不想你离开。 提交于 2019-12-10 13:28:36
问题 If I have a class A with only a copy constructor and a constructor with parameters int and int , and I place that class inside a class B : class B { public: B(); private A a; } How would I initialize a inside B's constructor? I've tried a(0, 0) , a = A(0, 0) , but not surprisingly neither worked, and I receive a error: no matching function for call to ‘A::A()’ 回答1: In B's constructor, you would do something like this: B::B() : a(0, 0) { // ctor here } 来源: https://stackoverflow.com/questions

Keep reference to new object passed into super constructor

微笑、不失礼 提交于 2019-12-10 13:03:11
问题 Is there a good way to maintain a reference to an object created in a constructor that needs to be passed to the super constructor of some extended class (other than making it accessible from the super class or passing it into the constructor as a parameter)? Let me clarify with an example. Take this class (which I can't modify and which doesn't give me access to foo): class TestA { TestA(Object foo) {} } Now, I would like to extends this class as follows: class TestB extends TestA { Object

Why am I getting a “does not contain a constructor that takes 0 arguments” error? C#

南笙酒味 提交于 2019-12-10 12:57:59
问题 On my form load, I have this code: private void Form1_Load(object sender, EventArgs e) { CharityCyclists cyclist1 = new CharityCyclists(); CharityCyclists cyclist2 = new CharityCyclists("a", 1, "Finished", 0, 0, 0, "One Wheel", 1, 500); cyclist1.Type = "Novelty Charity Cyclist"; cyclist1.Number = 1; cyclist1.Finished = "Not Finished"; cyclist1.Hours = 0; cyclist1.Mins = 0; cyclist1.Secs = 0; cyclist1.Bicycle = "Tricycle"; cyclist1.Wheels = 3; cyclist1.FundsRaised = 300; } However, I'm getting

Is there a way to derive from a class with an internal constructor?

我与影子孤独终老i 提交于 2019-12-10 12:46:35
问题 I'm working with a 3rd party c# class that has lots of great methods and properties - but as time has gone by I need to extend that class with methods and properties of my own. If it was my code I would just use that class as my base class and add my own properties and method on top - but this class has an internal constructor. (In my opinion it was short sited to make the constructor internal in the first place - why limit the ability to subclass?) The only thing I could think of was to

C# - closures over class fields inside an initializer?

我怕爱的太早我们不能终老 提交于 2019-12-10 12:46:04
问题 Consider the following code: using System; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { var square = new Square(4); Console.WriteLine(square.Calculate()); } } class MathOp { protected MathOp(Func<int> calc) { _calc = calc; } public int Calculate() { return _calc(); } private Func<int> _calc; } class Square : MathOp { public Square(int operand) : base(() => _operand * _operand) // runtime exception { _operand = operand; } private int _operand; } } (ignore

How to execute multiple constructor, when creating single object [duplicate]

纵饮孤独 提交于 2019-12-10 12:35:47
问题 This question already has answers here : How do I call one constructor from another in Java? (21 answers) Closed 6 years ago . I want to execute multiple constructor, while creating a single object. For example, I have a class definition like this- public class Prg { public Prg() { System.out.println("In default constructor"); } public Prg(int a) { System.out.println("In single parameter constructor"); } public Prg(int b, int c) { System.out.println("In multiple parameter constructor"); } }

How call constructor inside other constructor?

北城余情 提交于 2019-12-10 11:58:15
问题 My class has quite some properties and one of my constructors sets them all, I want the default constructor to call this other one and use the set properties. But I need to prepare arguments first, so calling it from the header won't help. Here's what I'd like to do: public class Test { private int result, other; // The other constructor can be called from header, // but then the argument cannot be prepared. public Test() : this(0) { // Prepare arguments. int calculations = 1; // Call