constructor

Can a C++ Class Constructor Know Its Instance Name?

Is it possible to know the object instance name / variable name from within a class method? For example: #include <iostream> using namespace std; class Foo { public: void Print(); }; void Foo::Print() { // what should be ????????? below ? // cout << "Instance name = " << ?????????; } int main() { Foo a, ...

weird C++ constructor/copy constructor issues in g++

#include <iostream> using namespace std; class X { public: X() { cout<<"Cons"<<endl; } X(const X& x){ cout<<"Copy"<<endl; } void operator=(const X& x){ cout<<"Assignment called";...

Why am I getting a stack overflow when using properties in the constructor?

I have a non-static class in which i have several properties, ie serverURL, serverPort etc, and the class has a constructor. The constructor accepts arguments which it then uses to 'set' the properties, initialising them. Here is the code: public Server(string newServerAddress, int newServerPort) { serverAddress = newServerAddr...

OO Javascript constructor pattern: neo-classical vs prototypal

I watched a talk by Douglas Crockford on the good parts in Javascript and my eyes were opened. At one point he said, something like, "Javascript is the only language where good programmers believe they can use it effectively, without learning it." Then I realized, I am that guy. In that talk, he made some statements that for me, were...

C# Ctor Can't New Com Obj ?

Hello all. I meet one strange issue. Public Class MyClass { Public MyClass() { // Some time the New Com Obj code will crush in Construcor su.SUEvent += new _IaSystemMgrEvents_SuEventEventHandler(su_SuEvent); su.SUEventSteps += new _IaSystemMgrEvents_SuEventIemsEventHandler(su_SuEventSteps); ...

C# constructor chaining? (How to do it?)

Hi all, I know that this is supposedly a super simple question, but I've been struggling with the concept for some time now. My question is, how do you chain constructors in c#? I'm in my first OOP class, so I'm just learning. I don't understand how constructor chaining works or how to implement it, or even why it's better than just do...

How to set an autoproperty in the constructor of a struct?

Why is this valid public struct MyStruct { public MyStruct(double value) { myField = value; } private double myField; public double MyProperty { get { return myField; } set { myField = value; } } } and this is not public stru...

Is there a Static Constrctors/Destructors Help topic

I know in D2010 they have added support for static constructors and destructors. Where I can find more information about they: syntax and samples? ...

How to chain these constructors (C#)?

Hi, I'm just getting the concept of chaining constructors down, but I can't figure out how to chain these two particular constructors together, so I would appreciate it if somebody could help me out. Thanks! Constructors // default constructor // purpose: initialize data members to zero // Parameters: none // returns: none public Li...

Should I use new Type() or just Type() for calling a constructor

Both syntaxes are equivalent (at least I suppose they are). let o1 = new Object() or let o2 = Object() Which way do you use more often? What about readability issues? ...

How do I pass in user inputed variables into a constructor?

Hi! I just need some help with this program. The user has to enter in the id,password,the number of max tries & the number of max uses. And they have to go into a constructor... Could someone help me pass them into the constructor? I'm using java. Thanks! import java.util.Scanner; public class LoginPw{ public static void main(String[...

Help Using Constructors for this situation? (C#)

Hello there, I'm in my first OOP class and I really like it, but on this assignment, I'm not really sure what the best (most efficient, less code, etc.) way to use constructors in this situation? Preferably using constructor chaining. A little more info: I would like my default constructor to create & initialize all the objects shown...

Boost.Python: Defining a constructor outside a class

Given a class: class TCurrency { TCurrency(); TCurrency(long); TCurrency(const std::string); ... }; Wrapped with Boost.Python: class_<TCurrency>( "TCurrency" ) .def( init<long> ) .def( init<const std::string&> ) ... ; Is it possible to create a factory method that appears as a constructor in Python: ...

Proper way to accomplish this construction using constructor chaining? (C#)

Hi, I have an assignment for my first OOP class, and I understand all of it including the following statement: You should create a class called ComplexNumber. This class will contain the real and imaginary parts of the complex number in private data members defined as doubles. Your class should contain a constructor that allows the...

C++ destruction of temporary object in an expression

Given the following code: #include <iostream> struct implicit_t { implicit_t(int x) : x_m(x) { std::cout << "ctor" << std::endl; } ~implicit_t() { std::cout << "dtor" << std::endl; } int x_m; }; std::ostream& operator<<(std::ostream& s, const implicit_t& x) { return s << x.x_m;...

List construction in Haskell

Hi I have the following recursive function for project euler question no. 74: chain n | n `elem` xs = length xs | otherwise = (chain (sumFac n)) : xs fac n = foldl (*) 1 $ enumFromTo 1 n sumFac n = sum $ map fac $ decToList n Except I don't know the correct syntax to construct a list on chain n so that it builds up a list of xs a...

Is there any reason to choose __new__ over __init__ when defining a metaclass?

I've always set up metaclasses something like this: class SomeMetaClass(type): def __new__(cls, name, bases, dict): #do stuff here But I just came across a metaclass that was defined like this: class SomeMetaClass(type): def __init__(self, name, bases, dict): #do stuff here Is there any reason to prefer one ...

How do you call the copy constructor within a member function of a class?

Here's what I've got: void set::operator =(const set& source) { if (&source == this) return; clear(); set(source); } And here's the error I get: vset.cxx:33: error: declaration of 'source' shadows a parameter How do I properly do this? ...

Pass by value or reference, to a C++ constructor that needs to store a copy?

Should a C++ (implicit or explicit) value constructor accept its parameter(s) by value or reference-to-const, when it needs to store a copy of the argument(s) in its object either way? Here is the shortest example I can think of: struct foo { bar _b; foo(bar [const&] b) // pass by value or reference-to-const? : _b(b) { ...

simulate virtual constructor in c++

Hi, In my application I have to derive some classes from a base one, the problem is that I want to enforce the derived classed to have 3 particular constructor implementation. As c++ don't have virtual pure constructor, it seemed quite desperate (I had to check manually each class implementation to ensure that the particular ctors are i...