Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create constructor and destructor for an anonymous class in C++.
I also want to release the memory before program goes out of scope.

eg.

#include <iostream>
using namespace std;
class
{
int *i;
public:
void setData(int x){ i = new int(x);}
void showData() { cout<<"i:"<<*i<<endl;}
}obj;

int main()
{
="" obj.setdata(10);
="" obj.showdata();
="" return="" 0;
}

<b="">What I have tried:

#include <iostream>
using namespace std;
class
{
int *i;
public:
void setData(int x){ i = new int(x);}
void showData() { cout<<"i:"<<*i<
Posted
Updated 16-Jan-22 2:47am
v2

 
Share this answer
 
Comments
Member 14036158 16-Jan-22 9:05am    
Thank you
OriginalGriff 16-Jan-22 10:08am    
You're welcome!
Anonymous Classes cannot have the followings:
    Cannot have a constructor or destructor.
    Cannot be passed as arguments to functions (unless type checking is defeated using ellipsis).
    Cannot be returned as return values from functions.

You can read complete story of Anonymous Classes: 
https://docs.microsoft.com/en-us/cpp/cpp/anonymous-class-types?view=msvc-170
 
Share this answer
 
Comments
Member 14036158 16-Jan-22 9:05am    
Thanks Imran.
M Imran Ansari 16-Jan-22 9:33am    
My pleasure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900