Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

Refering within the class to constructor with the this pointer

$
0
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 mystation 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 pointerthis to point to the function. I don't want to take the easy way out usingstation::station();

Many people are asking me why I would want to call the constructor from a function within my class. This is why

I want to 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. Below is my example for further clarity:

#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();
};



Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>