constructor

$_GET access inside __constructor() for PHP5

I am trying to populate a private data member inside a class via a $_GET parameter? Is this possible, or too complex for the constructor to handle? SOLVED *** - __construct was incorrectly typed /myfile.php?pid=f3f3rs2 class getvalue{ private $pid; function __constructor(){ $this->pid=$_GET['pid']; } public function get(){ ...

Throwing/catching exceptions from C'tor of a static object in C++

Hi all, I have a case in which I have to read an input file in the C'tor, but sometimes this file doesn't exist. This object is usually held statically, so its C'tor is called while loading the dll. I can't catch the exception I throw if the file doesn't exist because it's too early, and my executable crashes in an ugly way. I know it's...

Encapsulation in the age of frameworks

At my old C++ job, we always took great care in encapsulating member variables, and only exposing them as properties when absolutely necessary. We'd have really specific constructors that made sure you fully constructed the object before using it. These days, with ORM frameworks, dependency-injection, serialization, etc., it seems like...

Is there an implicit default constructor in C++?

In the book I'm reading at the moment (C++ Without Fear) it says that if you don't declare a default constructor for a class, the compiler supplies one for you, which "zeroes out each data member". I've experimented with this, and I'm not seeing any zeroing -out behaviour. I also can't find anything that mentions this on Google. Is this ...

Java programmer - how do C++ people use classes? Pointers to classes, default parameters?

I know my way around object-oriented programming, but I'm used to Java, and I never touched C++ until recently. I think my problem is not so much related to syntax as to the philosophy of OOP in C++. I understand the difference between pointers and addresses and the stack and the heap, and stuff, but I still feel like I'm missing som...

WPF: Loading data in the constructor of a UserControl breaks the Designer

I have a main window with a usercontrol. When adding code in the default constructor of the usercontrol, the designer stops showing the main window. It gives a message: Problem loading The document contains errors that must be fixed before the designer can be loaded. Reload the designer after you have fixed the errors. ...

Excel VBA object constructor and destructor

I need to make some custom objects in VBA that will need to reference each other and I have a some issues. First - how do object constructors work in VBA? Are there constructors? Second - are there destructors? How does VBA handle the end of the object lifecycle? If I have an object that references others (and this is their only refere...

how to setup private constructor?

how to setup the unit test if the constructor is private (.NET)? public class Class2 { private Class2() //private constructor { } public static Class2 getInstance() { if (x == null) x= new Class2(); return x; } } namespace TestUnit { [TestFixture] public class Class2Tester { private Class2 test; [SetUp()] pub...

Java Constructors

I am trying to learn how to specify class constructors in Java. I am starting to understand that they specify the types of instance variables of objects made from that class. They also can be used to set the instance variable initial values. The follow example is from the Java tutorial on Sun's website: public Bicycle(int startCadence, ...

Dynamic array... copy constructor, destructor, overloaded assignment operator

Hi, I am studying for my midterm exam. There is going to be a question about setting up an array dynamically, and maybe doing a copy constructor, a destructor and overloading the assignment operator. Can you please verify if I am correct. Also I don't understand what overloading the assignment operator means. Can you help me out with ...

Different arguments in the Constructor of a class in C++

Can I have a constructor work in different ways if the argument is of different type? i.e. int or float. Let's say that if I do, new Object(3) the constructor fills an array with 3 at every index Let's say that if I do, new Object(3.5) the constructor fills an array with index+3.5 for every index Let's say that if I do, new Object() ...

Best way to handle multiple constructors in Java

Hey, I've been wondering what the best (i.e. cleanest/safest/most efficient) way of handling multiple constructors in Java is? Especially when in one or more constructors not all fields are specified: public class Book { private String title; private String isbn; public Book() { //nothing specified! } p...

Where should I implement functionality in Page object - constructor or OnPreInit?

Hello all, A question I have been thinking about for a while - would Stackoverflow users commonly implement significant functionality in a constructor (specifically in classes derived from the System.Web.UI.Page class) , or should we keep the logic here as simple as possible and instead implement functionality in OnPreInit (using the co...

C++ constructor syntax

Simple question: are the following statements equivalent? or is the second one doing more implicit things behind the scenes (if so, what?) myClass x(3); myClass x = myClass(3); Thanks! ...

What are the best practices for determining the tasks of Constructor, Initialization and Reset methods

This is a general OOP question although I am designing in Java. I'm not trying to solve a particular problem, just to think through some design principles. From my experience I have reached the habit segregating object setup into three phases. The goal is to minimize: extra work, obfuscated code and crippled extensibility. Construction...

How does Python's "super" do the right thing?

I'm running Python 2.5, so this question may not apply to Python 3. When you make a diamond class hierarchy using multiple inheritance and create an object of the derived-most class, Python does the Right Thing (TM). It calls the constructor for the derived-most class, then its parent classes as listed from left to right, then the grandp...

What is the best way to initialize a bitfield struct in C++?

In C++, I have a class which contains an anonymous bitfield struct. I want to initialize it to zero without having to manually write out all fields. I can imagine putting the initialization in three places: Create a constructor in the bitfield Zero out in the initializer list of the constructor for the containing class Zero out in th...

Passing parameters to a delphi TFrame

To avoid singletons and global variables I'd like to be able to pass parameters to a TFrame component. However since a TFrame normally is included on form at design time it is only possible to use the default constructor. The parent form can of course set some properties in the OnCreate callback after the TFrame has been created. Howeve...

Interface defining a constructor signature?

It's weird that this is the first time I've bumped into this problem, but: How do you define a constructor in a C# interface? --Edit-- Some people wanted an example (it's a free time project, so yes, it's a game) IDrawable +Update +Draw To be able to Update (check for edge of screen etc) and draw itself it will always need a Graphi...

php5: To __construct() or to ClassName()?

Hey, I just started pondering this and figured I could use some input. I've long been using '__construct()' for all my constructor needs in php5, even though the obviously better looking 'ClassName()' would work just as well. I remember switching to __construct because it would forcibly break on older php installations but I don't think...