constructor

Is no parentheses on a C++ constructor with no arguments a language standard?

I was compiling a C++ program in Cygwin using g++ and I had a class whose constructor had no arguments. I had the lines: MyClass myObj(); myObj.function1(); And when trying to compile it, I got the message: error: request for member 'function1' in 'myObj', which is of non-class type 'MyClass ()()' After a little research, I found th...

Reusing constructors with final instance fields

Let's suppose that I have the following class which tries to be immutable public class Computation { private final Operation operation; private final double epsilon; public Computation(Operation operation) { this.operation = operation; //Default value epsilon = 0.01; } public Computat...

Does it make sense to create a repository that takes a default search criterium as constructor argument?

I'm refactoring a website I build. It's a website for a travel agency. I'm thinking of implementing the repository pattern. The travel agency has two divisions, that have their own website and target different groups of customers. The backend however is the same for both websites. They both access the same DB tables. Some tables simply ...

C++ constructors fun - constructing Foo with a copy of itself

I have: class Foo; class Bar { Foo foo; Bar(): foo(foo) {}; } Bar bar; At this point, is bar.foo // <--- how is this initialized? [This question arose from a buggy ref-counted pointer implemntation; I could have sworn that I ensured each pointer was pointing at something non-null; but I ended up with a pointer that pointed at...

Elementary C++ Type Confusion

I was reading the following text from Stanford's Programming Paradigms class, and I noticed that when the author uses the string class, the constructor does a function call that looks like this: string::string(const char* str) { initializeFrom(str, str + strlen(str)); } If the initializeFrom function takes two char* arguments, how...

how to specify Qt plugin constructor?

I wonder if it is possible to specify a constructor in a Qt plugin interface? (extending an app) I want to force the plugins using the interface to take a parameter in the constructor. ...

failed constructor and failed destructor in C++

Hi I have one question about failed constructor and failed destructor in C++. I noticed that when the constructor failed, an exception will be thrown. But there is no exception thrown in destructor. My question is 1) If constructor failed, what exception will be thrown? bad_alloc? or anything else related? Under what situation, a c...

assigning shared ptrs (boost) in constructor , unit testing

I have a C++ class(inside a dll project) whose member variables are boost::shared_ptrs to objects of other classes. Is it better to assign them inside the class constructor or have a separate init() function which does that. I am assuming the default value of pointer to T inside boost::shared_ptr is NULL. So if I do nothing inside the ...

trouble with StringTokenizer

Hi all, I'm getting the following error message and I can't seem to figure out the problem. Would really appreciate any help. The error message reads as:- BaseStaInstance.java:68: cannot find symbol symbol : constructor StringTokenizer(java.lang.Object,java.lang.String) location: class java.util.StringTokenizer ...

Variables after the colon in a constructor

I am still learning C++ and trying to understand it. I was looking through some code and saw: point3(float X, float Y, floatZ) : x(X), y(Y), z(Z) // <----- what is this used for { } What is the meaning of the "x(X), y(Y), z(Z)" sitting beside the constructor's parameters? ...

Flash: Pass constructor arguments to objects placed on stage?

Is it possible to pass constructor arguments to instance objects which I place on the stage? Are the instantiations of instance objects centralized somewhere as with .NET WinForms so I can just edit the xxx = new CustomRecangle() constructor? public class CustomRectangle extends MovieClip { public function CustomRectangle(...

Can Constructor Return a SubClass?

Given the following client code: var obj = new Class1(); Is there any way to modify the constructor of Class1 so that it will actually return a subclass (or some other alternate implementation) instead? I would like obj to get one of two different implementations, depending on some condition. Obviously, I could change to using a fac...

c# Emitting Dynamic Method Delegate to Load Parametrized Constructor Problem

I am trying create a delegate representation of constructor by emitting a Dynamic Method, which has to match this very "loosely-typed" signature so it can be used with any kind of parametrized constructor: public delegate Object ParamsConstructorDelegate(params object[] parameters); and the code for this creating the delegate looks li...

Implicitly invoking parent class initializer

class A(object): def __init__(self, a, b, c): #super(A, self).__init__() super(self.__class__, self).__init__() class B(A): def __init__(self, b, c): print super(B, self) print super(self.__class__, self) #super(B, self).__init__(1, b, c) super(self.__class__, self).__init__(1, b,...

Can a Java class have awareness of its instantiator?

Is there a way for a Java class to have an awareness of its instantiator? For example: public class Foo() { public Foo() { // can I get Bar.myInteger from here somehow // without passing it in to the constructor? } } public class Bar { private int myInteger; public Bar() { myInteger = 0; ...

Set properties of a class only through constructor

Hi , I am trying to make the properties of class which can only be set through the constructor of the same class ...

Where are my WCF client constructor overloads?

I'm creating a simple WCF service (MailService), and have added a service reference to a client project. The object browser in the client project is successfully showing the MailService metadata, including an object called MailServiceClient that has 4 overloads for its constructor. I want to use the single string parameter constructor, ...

Compiler complains about BOOST_CHECK_THROW on constructor

The following does not compile: class Foo { public: Foo( boost::shared_ptr< Bar > arg ); }; // in test-case boost::shared_ptr< Bar > bar; BOOST_CHECK_THROW( Foo( bar ), std::logic_error ); // compiler error here The implementation of Bar does not matter. The compiler complains, that Foo does not have an appropriate default cons...

C# Struct No Parameterless Constructor? See what I need to accomplish

I am using a struct to pass to an unmanaged DLL as so - [StructLayout(LayoutKind.Sequential)] public struct valTable { public byte type; public byte map; public byte spare1; public byte spare2; public int par; public int min; public byte[...

One constructor - multiple arguments

I've found a task on some Java programming competition. One must create class Sentence with only one argument 'text' and only one constructor. Here's sample test code : Sentence s1=new Sentence("only","CAT"), s2=new Sentence("and", 2, "mice"), s3=new Sentence(s1,s2,"completely","alone"), s4=new Sentence(s3, "on the ",...