constructor

Java: Creating a subclass object from a parent object

那年仲夏 提交于 2019-12-17 18:45:23
问题 Newbie Java question. Say I have: public class Car{ ... } public class Truck extends Car{ ... } Suppose I already have a Car object, how do I create a new Truck object from this Car object, so that all the values of the Car object is copied into my new Truck object? Ideally I could do something like this: Car c = new Car(); /* ... c gets populated */ Truck t = new Truck(c); /* would like t to have all of c's values */ Would I have to write my own copy constructor? This would have to be

Instantiate class with or without parentheses? [duplicate]

浪子不回头ぞ 提交于 2019-12-17 18:27:27
问题 This question already has answers here : Default constructor with empty brackets (9 answers) Closed last year . #include <iostream> using namespace std; class CTest { int x; public: CTest() { x = 3; cout << "A"; } }; int main () { CTest t1; CTest t2(); return 0; } CTest t1 prints "A" of course. But it seems like nothing happens at t2(), but the code runs well. So do we use those parentheses without argument? Or why can we use it this way? 回答1: This is a quirk of the C++ syntax. The line CTest

super() in constructor

怎甘沉沦 提交于 2019-12-17 18:03:21
问题 I'm reading through some code. In the constructor it has super() but the class implements interface which of course doesn't have a constructor. So which super() it is referring to? public class BoundingBox implements IBoundingVolume { public BoundingBox() { super(); mTransformedMin = new Number3D(); mTransformedMax = new Number3D(); mTmpMin = new Number3D(); mTmpMax = new Number3D(); mPoints = new Number3D[8]; mTmp = new Number3D[8]; mMin = new Number3D(); mMax = new Number3D(); for(int i=0;

Conditionally disabling a copy constructor

你。 提交于 2019-12-17 17:58:07
问题 Suppose I'm writing a class template C<T> that holds a T value, so C<T> can be copyable only if T is copyable. Normally, when a template might or might not support a certain operation, you just define the operation, and it's up to your callers to avoid calling it when it's not safe: template <typename T> class C { private: T t; public: C(const C& rhs); C(C&& rhs); // other stuff }; However, this creates problems in the case of a copy constructor, because is_copy_constructible<C<T>> will be

How useful would Inheriting Constructors be in C++?

半城伤御伤魂 提交于 2019-12-17 17:57:19
问题 As I sit in the C++ Standards committee meetings, they are discussing the pros and cons of dropping Inheriting Constructors since no compiler vendor has implemented it yet (the sense being users haven't been asking for it). Let me quickly remind everyone what inheriting constructors are: struct B { B(int); }; struct D : B { using B::B; }; Some vendors are proposing that with r-value references and variadic templates (perfect forwarding constructors), it would be trivial to provide a

Class constructor type in typescript?

一个人想着一个人 提交于 2019-12-17 17:43:07
问题 How can I declare a class type, so that I ensure the object is a constructor of a general class? In the following example, I want to know which type should I give to AnimalClass so that it could either be Penguin or Lion : class Animal { constructor() { console.log("Animal"); } } class Penguin extends Animal { constructor() { super(); console.log("Penguin"); } } class Lion extends Animal { constructor() { super(); console.log("Lion"); } } class Zoo { AnimalClass: class // AnimalClass could be

Is the default Move constructor defined as noexcept?

我怕爱的太早我们不能终老 提交于 2019-12-17 17:35:05
问题 It seems that a vector will check if the move constructor is labeled as noexcept before deciding on whether to move or copy elements when reallocating. Is the default move constructor defined as noexcept? I saw the following documentation but it didn't specify this.http://en.cppreference.com/w/cpp/language/move_constructor Implicitly-declared move constructor If no user-defined move constructors are provided for a class type (struct, class, or union), and all of the following is true: there

How do I initialize a stl vector of objects who themselves have non-trivial constructors?

蹲街弑〆低调 提交于 2019-12-17 17:32:25
问题 suppose I have the following class: class MyInteger { private: int n_; public: MyInteger(int n) : n_(n) {}; // MORE STUFF }; And suppose this class don't have a default trivial constructor MyInteger() . I must always supply an int to initialize it for some reason. And then suppose that somewhere in my code I need a vector<MyInteger> . How do I initialize each MyInteger component in this vector<> ? I have two situations (probably the solution is the same, but I'll state them anyway), a normal

Horrendous performance & large heap footprint of Java 8 constructor reference?

假如想象 提交于 2019-12-17 17:31:47
问题 I just had a rather unpleasant experience in our production environment, causing OutOfMemoryErrors: heapspace.. I traced the issue to my use of ArrayList::new in a function. To verify that this is actually performing worse than normal creation via a declared constructor ( t -> new ArrayList<>() ), I wrote the following small method: public class TestMain { public static void main(String[] args) { boolean newMethod = false; Map<Integer,List<Integer>> map = new HashMap<>(); int index = 0; while

virtual function calls in constructor and destructor [duplicate]

半世苍凉 提交于 2019-12-17 17:15:19
问题 This question already has answers here : Calling virtual functions inside constructors (13 answers) Closed 3 years ago . class Base { public: Base(){Foo();} ~Base(){Foo();} virtual void Foo(){std::cout<<"base";} }; class Derived: public Base { public: Derived(){Foo();} ~Derived(){Foo();} void Foo(){std::cout<<"derived";} }; //main { Derived d; } Any idea why this code prints out "base" and "derived"? I understand the advice is not to put virtual function calls inside constructor or