2,629 questions
452
votes
14
answers
156k
views
What is this weird colon-member (" : ") syntax in the constructor?
Recently I've seen an example like the following:
#include <iostream>
class Foo {
public:
int bar;
Foo(int num): bar(num) {};
};
int main(void) {
std::cout << Foo(42).bar << ...
235
votes
9
answers
43k
views
Why can't the default constructor be called with empty brackets?
Is there any good reason that an empty set of round brackets (parentheses) isn't valid for calling the default constructor in C++?
MyObject object; // ok - default ctor
MyObject object(blah); // ...
351
votes
9
answers
81k
views
Rule-of-Three becomes Rule-of-Five with C++11? [closed]
So, after watching this wonderful lecture on rvalue references, I thought that every class would benefit of such a "move constructor", template<class T> MyClass(T&& other) edit and of ...
276
votes
9
answers
178k
views
Why should I prefer to use member initializer lists?
I'm partial to using member initializer lists for my constructors, but I've long since forgotten the reasons behind this.
Do you use member initializer lists in your constructors? If so, why? If not, ...
407
votes
9
answers
155k
views
What's wrong with overridable method calls in constructors?
I have a Wicket page class that sets the page title depending on the result of an abstract method.
public abstract class BasicPage extends WebPage {
public BasicPage() {
add(new Label("...
1125
votes
8
answers
145k
views
Do the parentheses after the type name make a difference with new?
If Test is an ordinary class, is there any difference between:
Test* test = new Test;
and:
Test* test = new Test();
467
votes
10
answers
1.8m
views
Why do I get "TypeError: Missing 1 required positional argument: 'self'"?
I have some code like:
class Pump:
def __init__(self):
print("init")
def getPumps(self):
pass
p = Pump.getPumps()
print(p)
But I get an error like:
Traceback (...
51
votes
6
answers
9k
views
What is the reason to use the 'new' keyword at Derived.prototype = new Base
What does the following code do:
WeatherWidget.prototype = new Widget;
where Widget is a constructor, and I want to extend the Widget 'class' with a new function WeatherWidget.
What is the new ...
355
votes
12
answers
175k
views
Why is a call to a virtual member function in the constructor a non-virtual call?
Suppose I have two C++ classes:
class A
{
public:
A() { fn(); }
virtual void fn() { _n = 1; }
int getn() { return _n; }
protected:
int _n;
};
class B : public A
{
public:
B() : A() {}
...
201
votes
5
answers
74k
views
Is it bad practice to have a constructor function return a Promise?
I'm trying to create a constructor for a blogging platform and it has many async operations going on inside. These range from grabbing the posts from directories, parsing them, sending them through ...
3774
votes
10
answers
1.3m
views
What does the explicit keyword mean?
What does the explicit keyword mean in C++?
198
votes
12
answers
524k
views
Java default constructor
What exactly is a default constructor — can you tell me which one of the following is a default constructor and what differentiates it from any other constructor?
public Module() {
this.name = "";
...
894
votes
10
answers
1.1m
views
What are the rules for calling the base class constructor?
What are the C++ rules for calling the base class constructor from a derived class?
For example, I know in Java, you must do it as the first line of the subclass constructor (and if you don't, an ...
747
votes
23
answers
360k
views
Why do this() and super() have to be the first statement in a constructor?
Java requires that if you call this() or super() in a constructor, it must be the first statement. Why?
For example:
public class MyClass {
public MyClass(int x) {}
}
public class MySubClass ...
487
votes
36
answers
87k
views
Use of .apply() with 'new' operator. Is this possible?
In JavaScript, I want to create an object instance (via the new operator), but pass an arbitrary number of arguments to the constructor. Is this possible?
What I want to do is something like this (...