0
#include "stdafx.h"
ref class station{
public:
    station(){
    };
    void wrapper_1()
    {
        this->somefunct(); /*happy*/
    };
    void wrapper_2()
    {
        this->station(); /*not happy*/
    };
    void somefunct(){
        System::Console::WriteLine(L"abcde");
    };
};

int main(array<System::String^>^ args)
{
    station^ temp_1 = gcnew station();
    temp_1->wrapper_1();
    System::Console::ReadLine();
};

I want to use the this pointer to call my constructor within my station class, it doesn't like this and throws the following error:

error C2273: 'function-style cast' : illegal as right side of '->' operator.

Can someone explain to me how the constructor differs to other functions when using the pointer this to point to the function. I don't want to take the easy way out using station::station();

example of what I meant to @hans-passant

#include "stdafx.h"
ref class station{
public:
    station(int par_1,int par_2)
    {
        int sum = par_1 + par_2;
        System::Console::WriteLine(System::Convert::ToString(sum));
//default value output 13
    };
    station(){
        int pass_1 = 5;
        int pass_2 = 8;
        station(pass_1,pass_2); /* But why couldn't I use this->station(pass_1,pass_2);*/
    };
};
int main(array<System::String^>^ args)
{
    station^ obj = gcnew station();
    System::Console::ReadLine();
};
  • You cannot directly call the constructor, trying to do so is meaningless. The gcnew operator calls it. Which supplies the essential other essential ingredient, a pointer to the object storage that needs to be initialized by the constructor. The CLR does not offer a way to break this rule. – Hans Passant Apr 08 '14 at 13:24
  • @hans-passant unless I have an overloaded constructor so one constructor is parameterless and sets a default values to pass on to my main constructor because managed constructors parameters cannot contain have default values. And I wanted default values for my constructor parameters. – In euser3246323 Apr 08 '14 at 13:31
  • I supposed you are talking about "delegating constructors". Proposed as a future extension of C++/CLI in section F.3.1 of the language specification. C++11 implemented it for native C++. You couldn't come up with a worse example btw. – Hans Passant Apr 08 '14 at 13:36
  • Possible duplicate of [How to forward overloaded constructor call to another constructor in C++/CLI](http://stackoverflow.com/questions/2357076/how-to-forward-overloaded-constructor-call-to-another-constructor-in-c-cli/2357752#2357752). – Hans Passant Apr 08 '14 at 13:39
  • @hans-passant edit above is what I tried to describe. – In euser3246323 Apr 08 '14 at 13:54

0 Answers0