473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

C++ Help

Hi,
Tell me the concept.
How to restrict a class not to be inherite (in C++), but you can create an object of that class ???
Jul 18 '07 #1
1 1120
weaknessforcats
9,208 Expert Mod 8TB
Create two class definitions.

This one is used in main() and other user files as a header file:

Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.     private:
  4.         MyClass();
  5.     public:
  6.         static MyClass* Instance();
  7.         void AMethod();
  8.  
  9. };
  10.  
The constructor is private so you can't create on object in main().

However, the Instance() method is static and doesn't need an object. So you call it and that method returns the address a MyClass object, which is in another .cpp file.

In that other .cpp file you have:

Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.    public:
  4.     MyClass();
  5.     static MyClass* Instance();
  6.     void AMethod();
  7.  
  8. };
  9.  
  10. namespace
  11. {
  12.     MyClass obj;
  13. }
  14. MyClass* MyClass::Instance()
  15. {
  16.     return &obj;
  17. }
  18.  

In this file, the class defintion is different and is not in a header. That means it can only be used in this file where there is a global variable created in an anonymous namespace.

When you call the Instance() method from main() you call the function here and it returns the address of the global. You use that address in main() to call AMethod().

Inheritance is prohibited because the default constructor is private (and there are no other constructors in that header file) so any attempt to derive from this class will produce a compile error when you try to create the derived object.


This is an example of a Singletoin object. There is an article in the C/C++ Articles forum about Singleton objects.
Jul 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.