initialization-list

Python: Automatically initialize instance variables?

Hi, I have a python class that looks like this: class process: def __init__(self, PID, PPID, cmd, FDs, reachable, user): followed by: self.PID=PID self.PPID=PPID self.cmd=cmd ... Is there any way to autoinitialize these instance variables, like C++'s initialization list? It would spare lots of r...

C++ Initialization list and memory alloc.

Hi, Is the following valid? class myClass { private: ... int m_nDataLength; boost::shared_array<int> m_pData; ... public: myClass(): ..., m_nDataLength(10), m_pData(new int[m_nDataLength]), ... { } } Am I right in assuming that the initialization will happen exactly in the order I've given in ...

Benefits of Initialization lists

Of what I know of benefits of using initialization list is that they provide efficiency when initializing class members which are not build-in. For example, Fred::Fred() : x_(whatever) { } is preferable to, Fred::Fred() { x_ = whatever; } if x is an object of a custom class. Other than that, this style is used even wit...

Initialize parent's protected members with initialization list (C++)

Is it possible to use the initialization list of a child class' constructor to initialize data members declared as protected in the parent class? I can't get it to work. I can work around it, but it would be nice if I didn't have to. Some sample code: class Parent { protected: std::string something; } class Child : public Parent...

How to catch the exception in initialization list?

Hi I have a question about how to catch the exception in the initialization list. For example, we have a class Foo derived from Bar class Foo { public: Foo(int i) {throw 0; } } class Bar : public Foo{ public: Bar() : Foo(1) {} } ...

C++ Constructor initialization list strangeness

I have always been a good boy when writing my classes, prefixing all member variables with m_: class Test { int m_int1; int m_int2; public: Test(int int1, int int2) : m_int1(int1), m_int2(int2) {} }; int main() { Test t(10, 20); // Just an example } However, recently I forgot to do that and ended up writing: class Te...

Constructor initializer list: code from the C++ Primer, chapter 16

Toward the end of Chapter 16 of the "C++ Primer" I encountered the following code (I've removed a bunch of lines): class Sales_item { public: // default constructor: unbound handle Sales_item(): h() { } private: Handle<Item_base> h; // use-counted handle }; My problem is with the Sales_item(): h() { } line. For the sak...

C++: Initialize a member pointer to null?

I have a class that looks like: class Foo { public: Foo(); virtual ~Foo(); private: Odp* bar; }; I wish to initialize bar to NULL. Is this the best way to do it? Foo::Foo() : bar(NULL) { } Also, is it necessary that the destructor is virtual? (If that is true, then must the constructor be virtual as well?) ...

Writing a Prototype Constructor in C++

homework help time! I am taking a quadratic expression, where y=ax^2 + bx + c with a,b,c are constants and x is a variable. Here is my class: class quadratic { public: double evaluate(const double x); void getCoefficients (double &A, double &B, double &C); void setCoefficients (const double A, const double B, const double C); private:...

C++: Initialization of inherited field

Hello, I've a question about initialization of inherited members in constructor of derived class. Example code: class A { public: int m_int; }; class B: public A { public: B():m_int(0){} }; This code gives me the following output: In constructor 'B::B()': Line 10: error: class 'B' does not have any field na...