constructor

call one constructor from another in java

Hi This was the question asked in interview. Can we call one constructor from another if a class has multiple constructors in java and when?How can I call I mean syntax? ...

Best way to add a string based constructor to a Java class?

Say I have some class, e.g. Foo: public class Foo { private Integer x; private Integer y; public Foo(Integer x, Integer y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } } Now, I wish to add a constructor which takes as its argument a string representing ...

return type of the constructor in C++

I know that there is no return type of the constructors in C++ However, the code below compiles right. What is returned by the constructor in the code below? class A{ public: A() {} } A a = A(); //what is returned by A() here, why? Is there any conflict here? ...

One question about protected constructor

One question about protected constructor. I learnt that the protected constructor can be used in the derived class. How ever, I found the code below has an error. Why does it happen like this? class A { protected: A(){} }; class B: public A { public: B() { A* f=new A(); // Why it is not wor...

Multiple constructors definitions with same name but different signatures (C++)

With the following code, I keep getting error C2535 when I compile. It's complaining that a member function already defined or declared. Rationnel.h ... class Rationnel { public: Rationnel(int); //Constructor Rationnel(int,int); //Constructor void add(const Rationnel); ... Rationnel.cpp ... //Constructor Rationnel::Rationnel(int ...

Should a C++ constructor do real work?

Possible Duplicate: How much work should be done in a constructor? I'm strugging with some advice I have in the back of my mind but for which I can't remember the reasoning. I seem to remember at some point reading some advice (can't remember the source) that C++ constructors should not do real work. Rather, they should init...

Overload constructor for Scala's Case Classes?

In Scala 2.8 is there a way to overload constructors of a case class? If yes, please put a snippet to explain, if not, please explain why? ...

Is it OK to write a constructor which does nothing?

To use methods of a class I need to instantiate a class. At the moment the class has not constructor (so I want to write it). But than I have realized that the constructor should do nothing (I do need to specify values of fields). In this context I have a question if it is OK to write constructor which does nothing. For example: public...

How to start an Intent by passing some parameters to it?

I would like to pass some variables in the constructor of my ListActivity I start activity via this code: startActivity(new Intent (this, viewContacts.class)); I would like to use similar code, but to pass two strings to the constructor. How is possible? ...

Temporary non-const istream reference in constructor (C++)

It seems that a constructor that takes a non-const reference to an istream cannot be constructed with a temporary value in C++. #include <iostream> #include <sstream> using namespace std; class Bar { public: explicit Bar(std::istream& is) {} }; int main() { istringstream stream1("bar1"); Bar bar1(stream1); // OK on all platf...

C++: constructor initializer for arrays

I'm having a brain cramp... how do I initialize an array of objects properly in C++? non-array example: struct Foo { Foo(int x) { /* ... */ } }; struct Bar { Foo foo; Bar() : foo(4) {} }; array example: struct Foo { Foo(int x) { /* ... */ } }; struct Baz { Foo foo[3]; // ??? I know the following syntax is...

C++: Does the default constructor initialize built-in types

Does the default constructor (created by the compiler) initialize built-in-types? ...

Passing "this" in java constructor

Hi, Look into the following code: public class ClassA { private boolean ClassAattr = false; public ClassA() { ClassAHandler handler = new ClassAHandler(this); } } public class ClassAHandler extends GeneralHandler { ClassA ca = null; public ClassAHandler(ClassA classa) { this.ca = classa; }...

Can someone explain constructors in Java?

I'm learning Java by reading the online Java book, but im finding it hard to understand "constructors". My question is how would I write a constructor that sets private fields of the class to the values received? I've attempted this but don't think its correct: public Biscuit(String id, String Biscuitname, int numOfBiscuits); So if i...

C++ copy constructor function question

Hi, I wonder if there is something wrong with the copy constructor function below? class A { private: int m; public: A(A a){m=a.m} } Thanks and regards! ...

Copy constructor using private attributes

Hello all, My first question here so be gentle. I would like arguments for the following code: public class Example { private String name; private int age; ... // copy constructor here public Example(Example e) { this.name = e.name; // accessing a private attribute of an instance this.age = e.age;...

Is there any sense to declare default constructor in Java?

Is there any sense to declare default constructor in Java? class MyClass { public MyClass(){} public MyClass( T someArgs){ //somecode } } ...

g++ __static_initialization_and_destruction_0(int, int) - what is it

Hello After compiling of c++ file (with global static object) I get in nm output this function: 00000000 t _Z41__static_initialization_and_destruction_0ii __static_initialization_and_destruction_0(int, int) /* after c++filt */ What is it? It will call __cxa_atexit() Can I disable generation of this function (and calling a __cxa_...

When initializing in C# constructors what's better: initializer lists or assignment?

Class A uses an initializer list to set the member to the paramter value, while Class B uses assignment within the constructor's body. Can anyone give any reason to prefer one over the other as long as I'm consistent? class A { String _filename; A(String filename) : _filename(filename) { } } class B { String _f...

C++, how implicit conversion/constructor are determined?

How does C++ determine implicit conversion/construction of objects few levels deep? for example: struct A {}; struct B: A {}; struct C { operator B() { return B(); } }; void f(A a) {} int main(void) { f(C()); } Does it create tree of all possible conversions and chooses appropriate terminal? Something else? Thanks ...