constructor

Copy Constructors and calling functions

Hello, I'm trying to call an accessor function in a copy constructor but it's not working. Here's an example of my problem: A.h class A { public: //Constructor A(int d); //Copy Constructor A(const A &rhs); //accessor for data int getData(); //mutator for data void setData(int d); private: int d...

AJAX callback in Obj constructor

I have a class object called Location that works with Google in order to geocode a given address. The geocode request is made trough an AJAX call and handled via a callback that will initiate the class members once the response arrives. Here is the code: function Location(address) { this.geo = new GClientGeocoder(); this.addres...

Is there any way to manipulate variables passed in to a child class constructor before passing it off to the base class?

Hi, Is there any way to delay calling a superclass constructor so you can manipulate the variables first? Eg. public class ParentClass { private int someVar; public ParentClass(int someVar) { this.someVar = someVar; } } public class ChildClass : ParentClass { public ChildClass(int someVar) : base(someVar) ...

Ambiguous constructor call

I'm trying to create a simple date class, but I get an error on my main file that says, "call of overloaded Date() is ambiguous." I'm not sure why since I thought as long as I had different parameters for my constructor, I was ok. Here is my code: header file: #ifndef DATE_H #define DATE_H using std::string; class Date { public: ...

C++ Array Initialization in Function Call or Constructor Call

This question is related to the post here. Is it possible to initialize an array in a function call or constructor call? For example, class foo's constructor wants an array of size 3, so I want to call foo( { 0, 0, 0 } ). I've tried this, and it does not work. I'd like to be able to initialize objects of type foo in other objects' constr...

Definition of variables/fields type within a constructor, how is it done?

I just had a look at Suns Java tutorial, and found something that totally confused me: Given the following example: public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } Why is it, that the types of the variables (fields?) gear, cadence and speed do not nee...

Constructor Type Coercion in C++

Take the following class: class mytype { double num; public: mytype(int a) { num = sqrt(a); } void print() { cout << num; } } Say there is a method which takes a mytype: void foo(mytype a) { a.print(); } Is it legal c++ (or is there a way to implemen...

Constructor versus setter injection

Hi, I'm currently designing an API where I wish to allow configuration via a variety of methods. One method is via an XML configuration schema and another method is through an API that I wish to play nicely with Spring. My XML schema parsing code was previously hidden and therefore the only concern was for it to work but now I wish to ...

Is it safe to call a function to intialize a class in a ctor list?

I have Angle class that I want initialized to a random value. The Angle constructor can accept an int from a random() function. Is it safe to place this call in the ctor list: foo::foo() : Angle(random(0xFFFF)) {...} or do I have to do it in the body of the constructor? foo::foo() { Angle = Angle(random(0xFFFF)); ...} If it matte...

Doubt in abstract classes

public abstract class Person { private String name; public Person(String name) { this.name = name; System.out.println("Person"); } public String getName() { return name; } abstract public String getDescription(); } public class Student extends Person { private String major; ...

[C#] How to create a constructor of a class that return a collection of instances of that class?

My program has the following class definition: public sealed class Subscriber { private subscription; public Subscriber(int id) { using (DataContext dc = new DataContext()) { this.subscription = dc._GetSubscription(id).SingleOrDefault(); } } } ,where _GetS...

Can I refer to an object in its constructor?

Can I do the following? public Manager(String userName) { game = new Game(userName); game.addManager(this); } The problem is that I refer to an object (this) in its constructor (before it was actually created). ...

C++ constructor behavior

I'm declaring an instance of a class like so: Matrix m; This appears to implicitly initialize m (i.e. run the constructor). Is this actually the case? ...

Why should the copy constructor accept its parameter by reference in C++?

Why copy constructor must be passed its parameter by reference? ...

CA2000 passing object reference to base constructor in C#

I receive a warning when I run some code through Visual Studio's Code Analysis utility which I'm not sure how to resolve. Perhaps someone here has come across a similar issue, resolved it, and is willing to share their insight. I'm programming a custom-painted cell used in a DataGridView control. The code resembles: public class DataGr...

Why cant we create Object if constructor is in private section?

Dear all I want to know why cant we create object if the constructor is in private section. I know that if i make a method static i can call that method using <classname> :: <methodname(...)>; But why can't we create object is what I don't understand. I also know if my method is not static then also I can call functio...

Constructor initialising an array of subobjects?

Say I have several objects within a class, each of which needs constructing with a different value. I can write something like this: class b { public: b(int num) { // 1 for a.b1, and 2 for a.b2 } }; class a { public: b b1; b b2; a() : b1(1), b2(2) { } }; However, is it possible to do the same thing if tho...

Handling Exceptions that happen in a asp.net MVC Controller Constructor

What's the best way to handle exceptions that happen from within a controller's constructor? All I can think of to do is use Application_OnError() or put a try/catch in my ControllerFactory. Neither of these solutions seem ideal. Application_OnError is to broad - I have some non-mvc content in the site that has its own error handling. ...

Can C++ Constructors be templates?

Hi, I have non-template class with a templatized constructor. This code compiles for me. But i remember that somewhere i have referred that constructors cannot be templates. Can someone explain whether this is a valid usage? typedef double Vector; //enum Method {A, B, C, D, E, F}; struct A {}; class Butcher { public: template <cla...

Calling child constructor by casting (ChildClass)parentObject; to track revisions

To track revisions of a Page class, I have a PageRevision class which inherits from Page and adds a revision ID (Guid RevisionID;). If possible, how should I cast an existing Page object to a PageRevision and ensure that the PageRevision constructor is called to create a new revision ID? I could could have a PageRevision(Page page) con...