Constructor issue

A

Armin Zingler

Hi,

how can I translate this VB code to managed C++? Note that the base class'
constructor is not called inside C2.New(i), neither implicitly nor
explicitly. In a constructor, it is valid to call an overloaded constructor
as the first statement instead of calling the base class' constructor. I
don't know the correct syntax in C++.

Class C1
Sub New(ByVal i As Integer)
End Sub
End Class

Class C2
Inherits C1
Sub New(ByVal i As Integer, ByVal j As Integer)
MyBase.New(i)
End Sub
Sub New(ByVal i As Integer)
MyClass.New(i, 0)
End Sub
End Class


My current version is:

ref class C1
{
public:
C1(int i)
{
}
};

ref class C2: C1
{
public:
C2 (int i, int j): C1(i)
{
};

C2 (int i) // C2512: 'C1': no appropriate
// default constructor available
{
C2(i, 0);
}
};

See C2512 inline. I've tried to change it to

C2 (int i): C2 (i,0)
{
}

but that's not the correct syntax, too. So, how is it done correctly?




Armin
 
D

David Wilkinson

Armin said:
Hi,

how can I translate this VB code to managed C++? Note that the base class'
constructor is not called inside C2.New(i), neither implicitly nor
explicitly. In a constructor, it is valid to call an overloaded
constructor as the first statement instead of calling the base class'
constructor. I don't know the correct syntax in C++.

Class C1
Sub New(ByVal i As Integer)
End Sub
End Class

Class C2
Inherits C1
Sub New(ByVal i As Integer, ByVal j As Integer)
MyBase.New(i)
End Sub
Sub New(ByVal i As Integer)
MyClass.New(i, 0)
End Sub
End Class


My current version is:

ref class C1
{
public:
C1(int i)
{
}
};

ref class C2: C1
{
public:
C2 (int i, int j): C1(i)
{
};

C2 (int i) // C2512: 'C1': no appropriate
// default constructor available
{
C2(i, 0);
}
};

See C2512 inline. I've tried to change it to

C2 (int i): C2 (i,0)
{
}

but that's not the correct syntax, too. So, how is it done correctly?

Are you looking for something like

ref class C2: C1
{
public:
C2 (int i, int j): C1(i)
{
Init(j);
}

C2 (int i): C1(i)
{
Init(0);
}

private
void Init(int j)
{
// initialization code goes here
}
};

Or you can use default argument:

ref class C2: C1
{
public:
C2 (int i, int j = 0): C1(i)
{
// initialization goes here
}
};
 
A

Armin Zingler

David said:
Are you looking for something like

ref class C2: C1
{
public:
C2 (int i, int j): C1(i)
{
Init(j);
}

C2 (int i): C1(i)
{
Init(0);
}

private
void Init(int j)
{
// initialization code goes here
}
};

Or you can use default argument:

ref class C2: C1
{
public:
C2 (int i, int j = 0): C1(i)
{
// initialization goes here
}
};

Both should work. Thanks. But does this mean I can not call one constructor
from the other like in VB? Sometimes, optional arguments can not be used,
like here: (without inheritance this time)

ref class C3
{
private:
int _i;
String^ _s;
double _d;
public:
C3(int i) {_i = i};
C3(int i, String^ s)
{
C3(i); // C2082: redefinition of formal parameter 'i'
_s = s
};
C3(int i, double d)
{
C3(i); // C2082: redefinition of formal parameter 'i'
_d = d
};
}

I mean, in practice I often have one "base" constructor called by others.
I'd have to use your first approach then, right?


Armin
 
A

Armin Zingler

Class C1
BTW, the latter constructor is translated to

IL_0001: ldarg.0
IL_0002: ldarg.1
IL_0003: ldc.i4.0
IL_0004: call instance void WindowsApplication1.C2::.ctor(int32,
int32)

To me this means it's just managed C++ that doesn't allow calling one
constructor from the other, right?


Armin
 
D

David Anton

Same class constructor chaining is not allowed in C++, managed or unmanaged.
It's in C++0X, whenever that's ready and your favorite compiler implements it.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB
 
A

Armin Zingler

David said:
Or you can use default argument:

ref class C2: C1
{
public:
C2 (int i, int j = 0): C1(i)
{
// initialization goes here
}
};

Little problem:
"C3222: cannot declare default arguments for member functions of a managed
type or generic functions."

Are there optional parameters in managed C++?
I've looked for "optional" (the VB keyword) here
http://msdn.microsoft.com/en-us/library/zwkz3536.aspx
but it's not listed.


Armin
 
B

Ben Voigt [C++ MVP]

To me this means it's just managed C++ that doesn't allow calling one
constructor from the other, right?

You're right, C++ doesn't allow this. Usually Microsoft creates a new
syntax for .NET features which are different from standard C++, but as David
points out, this feature is planned for standard C++, and Microsoft wanted
to wait and use the same syntax as the standard, not introduce an alternate
and incompatible syntax.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top