• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Final class

How do I make a final class like in Java?
0
ROWANBRIGHTON
Asked:
ROWANBRIGHTON
  • 2
1 Solution
 
Sys_ProgCommented:
There isn't any direct way in the language as such of doing it....but yeah, u could force it thru an indeirect method

Make the constructor private as below


class FinalClass {
public:
     static FinalClass * Create_FinalClass();
private:
     FinalClass(const FinalClass&);
     FinalClass& operator=(const FinalClass&);
     FinalClass(){}
};

FinalClass* FinalClass::Create_FinalClass() {
     return new FinalClass();
}

int main(int argc, char* argv[]) {
     FinalClass *f = FinalClass::Create_FinalClass();    
     delete f;
     system("pause");
     return 0;
}

Amit
0
 
AxterCommented:
Just to add to above comment, you can use an auto_ptr so the class gets deleted automatically.

#include <memory>

void SomeFunct()
{
      std::auto_ptr<FinalClass> f(FinalClass::Create_FinalClass());
      f->Some_func();
}
0
 
ROWANBRIGHTONAuthor Commented:
Thanks sys_Prog
0
 
Sys_ProgCommented:
U r welcome
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now