constructor

How to write a simple class in C++?

天大地大妈咪最大 提交于 2019-12-17 22:35:52
问题 I have been reading a lot of tutorials on C++ class but they miss something that other tutorials include. Can someone please show me how to write and use a very simple C++ class that uses visibility, methods and a simple constructor and destructor? 回答1: Well documented example taken and explained better from Constructors and Destructors in C++: #include <iostream> // for cout and cin class Cat // begin declaration of the class { public: // begin public section Cat(int initialAge); //

Why is a POD in a struct zero-initialized by an implicit constructor when creating an object in the heap or a temporary object in the stack?

左心房为你撑大大i 提交于 2019-12-17 22:33:04
问题 The standard and the C++ book say that the default constructor for class type members is called by the implicit generated default constructor, but built-in types are not initialized. However, in this test program I get unexpected results when allocating an object in the heap or when using a temporary object: #include<iostream> struct Container { int n; }; int main() { Container c; std::cout << "[STACK] Num: " << c.n << std::endl; Container *pc = new Container(); std::cout << "[HEAP] Num: " <<

What does “this()” method mean?

喜夏-厌秋 提交于 2019-12-17 22:13:13
问题 I ran into this block of code, and there is this one line I don't quit understand the meaning or what it is doing. public Digraph(In in) { this(in.readInt()); int E = in.readInt(); for (int i = 0; i < E; i++) { int v = in.readInt(); int w = in.readInt(); addEdge(v, w); } } I understand what this.method() or this.variable are, but what is this() ? 回答1: This is constructor overloading: public class Diagraph { public Diagraph(int n) { // Constructor code } public Digraph(In in) { this(in.readInt

Java constructor with large arguments or Java bean getter/setter approach

戏子无情 提交于 2019-12-17 21:51:54
问题 I can't decide which approach is better for creating objects with a large number of fields (10+) (all mandatory) the constructor approach of the getter/setter. Constructor at least you enforce that all the fields are set. Java Beans easier to see which variables are being set instead of a huge list. The builder pattern DOES NOT seem suitable here as all the fields are mandatory and the builder requires you put all mandatory parameters in the builder constructor. Thanks, D 回答1: The better

Why can't this() and super() both be used together in a constructor?

蓝咒 提交于 2019-12-17 21:46:41
问题 Why can't this() and super() both be used together in a constructor? What is the reason for incorporating such a thing? 回答1: this(...) will call another constructor in the same class whereas super() will call a super constructor. If there is no super() in a constructor the compiler will add one implicitly. Thus if both were allowed you could end up calling the super constructor twice. Example (don't look for a sense in the parameters): class A { public A() { this( false ); } public A(boolean

What (not) to do in a constructor

◇◆丶佛笑我妖孽 提交于 2019-12-17 21:45:42
问题 I want to ask you for your best practices regarding constructors in C++. I am not quite sure what I should do in a constructor and what not. Should I only use it for attribute initializations, calling parent constructors etc.? Or might I even put more complex functions into them like reading and parsing configuration data, setting up external libraries a.s.o. Or should I write special functions for this? Resp. init() / cleanup() ? What are the PRO's and CON's here? I figured out yet that for

C++ default destructor

柔情痞子 提交于 2019-12-17 21:44:46
问题 When I don't declare a constructor for example, the compiler will provide me with a default constructor that will have no arguments and no definition (body), and thus, will take no action . If I now don't declare a destructor , the compiler will provide me with a default destructor with no defintion (body), and thus, I think no action . So, if I'm finished with an object for example, wouldn't the default destructor reallocate (free) memory used by the object? If it doesn't, why are we getting

StackOverFlowError when initializing constructor for a list

帅比萌擦擦* 提交于 2019-12-17 21:22:52
问题 I'm confused about why I keep getting a java.lang.StackOverFlow error when I try to write a constructor for MyArrayList, a personal version of the arraylist class in java. I know StackOverFlow errors happen when there is a recursive call, but that doesn't appear to be the case for the code I have written? Can anybody help explain why I'm getting this error? This is my code, I've included all of the constructors I have written, but the first MyArrayList() is the one that the error indicates in

May I call a virtual function to initialize a base-class sub-object?

血红的双手。 提交于 2019-12-17 21:09:14
问题 I know that virtual functions should not be called either directly or indirectly in a constructor, but this code runs fine. Is what I have here safe? #include <iostream> #include <string> struct A { A (const std::string& name) {std::cout << name << std::endl;} virtual std::string tag() const = 0; }; struct B: A { B() : A (tag()) {} virtual std::string tag() const override {return "B";} }; int main() { B b; // Output gives "B\n" } If not, would the following (based on a comment) be a correct

Java — private constructor vs final and more

浪尽此生 提交于 2019-12-17 20:58:02
问题 Suppose there is a class with all of its constructors declared as private. Eg.: public class This { private This () { } public someMethod( ){ // something here } // some more-- no other constructors } From what I know, making all constructors private is similar to declaring the class "This" as final -- so that it can't be extended. However, the Eclipse messages i'm getting are giving me the impression that this is possible-- an all-constructors-private class can be extended. Take a look at