constructor

Visibility of nester class constructor

Is there a way to limit the instantiation of the nested class in C#? I want to prevent nested class being instantiated from any other class except the nesting class, but to allow full access to the nested class from other code. ...

Why does a sub-class class of a class have to be static in order to initialize the sub-class in the constructor of the class?

So, the question is more or less as I wrote. I understand that it's probably not clear at all so I'll give an example. I have class Tree and in it there is the class Node, and the empty constructor of Tree is written: public class RBTree { private RBNode head; public RBTree(RBNode head,RBTree leftT,RBTree rightT){ this.head=head;...

Relevance of 'public' constructor in abstract class.

Is there any relevance of a 'public' constructor in an abstract class? I can not think of any possible way to use it, in that case shouldn't it be treated as error by compiler (C#, not sure if other languages allow that). Sample Code: internal abstract class Vehicle { public Vehicle() { } } The C# compiler all...

Issue calling superclass method in subclass constructor

I get a NullPointerException calling a Superclass Method in Subclass Inner Class Constructor... What's the Deal? In my application's main class (subclass of Application), I have a public inner class that simply contains 3 public string objects. In the parent class I declare an object of that inner class. public class MainApplication ...

How are Flash library symbols constructed? Why are width/height already available in constructor?

Suppose I draw a square on the stage, convert it to a symbol, export it for ActionScript with a classname of "MySquare" (and of course a base class of MovieClip). How is it that in the MySquare constructor, the width and height of this MovieClip are already available? In fact, any named instances in the clip are also available. I'm co...

Calling methods in super class constructor or subclass constructor?

1. Passing configuration to the __init__ method which calls register implicitely: class Base: def __init__(self, *verbs): if not verbs: verbs = "get", "post" self._register(verbs) def _register(self, *verbs): pass class Sub(Base): def __init__(self): super().__init__("get", "pos...

C++ LNK2019 error with constructors and destructors in derived classes

I have two classes, one inherited from the other. When I compile, I get the following errors: Entity.obj : error LNK2019: unresolved external symbol "public: __thiscall Utility::Parsables::Base::Base(void)" (??0Base@Parsables@Utility@@QAE@XZ) referenced in function "public: __thiscall Utility::Parsables::Entity::Entity(void)" (??0Ent...

Forcing a templated object to construct from a pointer

I have a fictional class: template<typename T> class demonstration { public: demonstration(){} ... T *m_data; } At some point in the program's execution, I want to set m_data to a big block of allocated memory and construct an object T there. At the moment, I've been using this code: void construct() { *m_data ...

Better to use constructor or method factory pattern?

I have a wrapper class for the Bitmap class called BitmapZone. Assuming we have a WIDTH x HEIGHT bitmap picture, this wrapper class should serve the purpose of allowing me to send to other methods/classes itself instead of the original bitmap. I can then better control what the user is or not allowed to do with the picture (and I don't ...

Assignment operator that calls a constructor is broken

I've implemented some of the changes suggested in this question, and (thanks very much) it works quite well, however... in the process I've seemed to break the post-declaration assignment operator. With the following code: #include <cstdio> #include "ucpp" main() { ustring a = "test"; ustring b = "ing"; ustring c = "- -"; ustrin...

Good style for handling constructor failure of critical object

I'm trying to decide between two ways of instantiating an object & handling any constructor exceptions for an object that is critical to my program, i.e. if construction fails the program can't continue. I have a class SimpleMIDIOut that wraps basic Win32 MIDI functions. It will open a MIDI device in the constructor and close it in the...

Pattern for database-wrapper in java

I am currently writing a java-class that wraps an SQLite database. This class has two ways to be instantiated: Open an existing database. Create a new database. This is what I came up with: public class SQLiteDatabaseWrapper { public static SQLiteDatabaseWrapper openExisting(File PathToDB) { return new SQLiteDatabaseWrap...

C# - Adding to an existing (generated) constructor

I have a constructor that is in generated code. I don't want to change the generated code (cause it would get overwritten when I regenerate), but I need to add some functionality to the constructor. Here is some example code: // Generated file public partial class MyGeneratedClass { public MyGeneratedClass() { Does some g...

C++ Singleton Constructor and Destructor

Does it matter if the constructor/destructor implementation is provided in the header file or the source file? For example, which way is preferred and why? Way 1: class Singleton { public: ~Singleton() { } private: Singleton() { } }; Way 2: class Singleton { public: ~Singleton(); private: Singleton(); }; ...

Using member functions of members in the constructor initializer

I've run into the following a few times with initializer lists and I've never been able to explain it well. Can anyone explain why exactly the following fails (I don't have a compiler to catch typos, so bear with me): class Foo { public: Foo( int i ) : m_i( i ) {} //works with no problem int getInt() {return m_i;} ~Foo() {} ...

For structs, do I have to call the constructor explicitly in C#?

The question is about the structs. When I declare a struct type variable/object (don't know which one suits better) or an array or list of structs, do I have to call the constructor explicitly like objects or just declaring will suffice like variables? ...

C++: Constructor/destructor unresolved when not inline?

In a plugin-based C++ project, I have a TmpClass that is used to exchange data between the main application and the plugins. Therefore the respective TmpClass.h is included in the abstract plugin interface class that is included by the main application project, and implemented by each plugin. As the plugins work on STL vectors of TmpCla...

calling constructor of the class in the destructor of the same class

Experts !! I know this question is one of the lousy one , but still I dared to open my mind , hoping I would learn from all. I was trying some examples as part of my routine and did this horrible thing, I called the constructor of the class from destructor of the same class. I don't really know if this is ever required in real programm...

Perl - Calling subclass constructor from superclass (OO)

This may turn out to be an embarrassingly stupid question, but better than potentially creating embarrassingly stupid code. :-) This is an OO design question, really. Let's say I have an object class 'Foos' that represents a set of dynamic configuration elements, which are obtained by querying a command on disk, 'mycrazyfoos -getconf...

Foo f = Foo(); // no matching function for call to 'Foo::Foo(Foo)' ... huh?!

class Foo { public: explicit Foo() {} explicit Foo(Foo&) {} }; Foo d = Foo(); error: no matching function for call to 'Foo::Foo(Foo)' I tried changing Foo(Foo&) to Foo(Foo) as the error suggests, which AFAIK is not a valid constructor, and sure enough I get: error: invalid constructor; you probably meant ‘Foo (const F...