Sponsored Content
Top Forums Programming static use for class inside the same class c++ Post 302684409 by xavipoes on Thursday 9th of August 2012 02:17:11 PM
Old 08-09-2012
Data static use for class inside the same class c++

Hi,
I believe the next code is wrong:

Code:
class Egg {
    Egg e;
    int i;
    Egg(int ii=0) : i(ii) {}
};

because you would end up with an endless definition (memory allocation) of Egg objects, thus int i.
Ok, so God Eckel proposes for a singleton:
Code:
class Egg {
    static Egg e;
    int i;
    Egg(int ii=0) : i(ii) {}
};
Egg Egg::e(47);

Does the static qualifier prevent from the memory allocation for the endless series of int i? I am not quite sure because any new Egg object (thus, int i) is in an inner scope (e.e...), even static says that all Egg objects share the same static memory for that member. ???
I understand that making the constructor private prevents the class user from creating any object, leaving us only with the shared static data member, previously defined by the class creator. Right?
Thanks for reading
Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.
 

10 More Discussions You Might Find Interesting

1. Programming

Unix Class

I'm trying to put together a summer class for me and a few of my classmates. The class will be given a credit this fall. The Topic will be Unix Network Programming for Unix. We will have some IBM R6000's to work on, with AIX 4.2 and/or yellow dog (as yet to be determined if/how much a role each... (1 Reply)
Discussion started by: thmnetwork
1 Replies

2. UNIX Desktop Questions & Answers

It's first class!

This is a great sight! Excellent site, added to favorites!! I am also webmaster and I like this one!Thank you! (0 Replies)
Discussion started by: Leksej
0 Replies

3. UNIX Desktop Questions & Answers

It's first class!

Hi..!!!You have a very nice web-site! Good work!I am also webmaster and I like this one!Thank you! (1 Reply)
Discussion started by: Hardan
1 Replies

4. UNIX for Dummies Questions & Answers

car class (not school class)

im just trying to have some fun and kill some time writing a c++ program that has a person type in a car make and model then gives them a year and a price. or something like that. i always have problems getting it goin but once the ball is rolling im usually pretty good. anyone wanna help me out? ... (1 Reply)
Discussion started by: rickym2626
1 Replies

5. Programming

C++ class definition with a member of the same class

Hi, i have a question about C++. Is it possible to declare a class with a member ot the same class? For example, a linked list or i want to convert this C code to C++ class (Elemento) typedef struct elemento { char name; char value; List<struct elemento> ltElementos; ... (7 Replies)
Discussion started by: pogdorica
7 Replies

6. Shell Programming and Scripting

Perl class help

cat a.pl #!/usr/bin/perl package Myperlclass; sub mysub{ my $class; $class=shift; my $self{}; bless $self,$class; return $self; } sub add{ my $a,$b,$res; $a=$_; $b=$_; $res=$a+$b; print "res: $res\n"; } (0 Replies)
Discussion started by: cola
0 Replies

7. UNIX for Advanced & Expert Users

Get pointer for existing device class (struct class) in Linux kernel module

Hi all! I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. I can create a class in a module, get the pointer to it and then use it to register the device with: *cl = class_create(THIS_MODULE, className);... (0 Replies)
Discussion started by: hdaniel@ualg.pt
0 Replies

8. Programming

Size of Derived class, upon virtual base class inheritance

I have the two class definition as follows. class A { public: int a; }; class B : virtual public A{ }; The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Discussion started by: techmonk
2 Replies

9. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies

10. Programming

Array initialization inside class in C++

const int VALUES = {7,4,2,1,0}; //or int VALUES = {7,4,2,1,0};this statement inside a class definition gives error. Why? (3 Replies)
Discussion started by: milhan
3 Replies
SoMemoryError(3)						       Coin							  SoMemoryError(3)

NAME
SoMemoryError - The SoMemoryError class is used to inform of problems with memory allocation. Modern operating systems takes care of handling most out of memory conditions for you, but in certain situations it can be wise to do some manual checking and intervention. This class is provided as an aid to help out in these situations. SYNOPSIS
#include <Inventor/errors/SoMemoryError.h> Inherits SoError. Public Member Functions virtual SoType getTypeId (void) const Static Public Member Functions static void setHandlerCallback (SoErrorCB *const callback, void *const data) static SoErrorCB * getHandlerCallback (void) static void * getHandlerData (void) static SoType getClassTypeId (void) static void post (const char *const whatWasAllocated) static void initClass (void) Protected Member Functions virtual SoErrorCBPtr getHandler (void *&data) const Additional Inherited Members Detailed Description The SoMemoryError class is used to inform of problems with memory allocation. Modern operating systems takes care of handling most out of memory conditions for you, but in certain situations it can be wise to do some manual checking and intervention. This class is provided as an aid to help out in these situations. The basic design of the Coin library is to pass on the responsibility for handling failed attempts at memory allocation to the application programmer. If you want to detect and take care of these, you should compile Coin with the C++ exception throwing on and wrap your code within try{} and catch{} blocks. The most you can do if you get a failed allocation is typically to notify the user and then exit the application, though, and this is something most operating systems will do for you, so you probably need not consider this at all. So, where does the SoMemoryError class come in handy? There are certain things which could happen within the library which are best taken care of by internally handling failed attempts at memory allocation. An example: the user tries to load a model file which contains a filename pointer to a huge bitmapfile with a texture map. The end-user's system does not provide enough memory to load the file and prepare the texture image for rendering, though. This is a case where it is possible to just emit a warning and continue. The warning will then be passed through this class. Note that SoMemoryError is probably not of much use to the application programmer. Member Function Documentation void SoMemoryError::setHandlerCallback (SoErrorCB *constfunction, void *constdata) [static] This method sets the error handler callback for messages posted via this class. Note that this will not override the error/debug message handler for subclasses, these will have to be overrided by calling the subclass' setHandlerCallback() method. See also: defaultHandlerCB() Reimplemented from SoError. SoErrorCB * SoMemoryError::getHandlerCallback (void) [static] Returns the error handler callback for messages posted via this class. Reimplemented from SoError. void * SoMemoryError::getHandlerData (void) [static] This method returns the pointer used for passing data back to the callback handler method. Reimplemented from SoError. SoType SoMemoryError::getClassTypeId (void) [static] This static method returns the SoType for this class. See also: getTypeId() Reimplemented from SoError. SoType SoMemoryError::getTypeId (void) const [virtual] This method returns the SoType of a particular object instance. See also: getClassTypeId() Reimplemented from SoError. void SoMemoryError::post (const char *constwhatWasAllocated) [static] Posts a warning about a failed memory allocation. whatWasAllocated should contain a description of what we tried to allocate. void SoMemoryError::initClass (void) [static] This method takes care of initializing all static data for the class. Reimplemented from SoError. SoErrorCB * SoMemoryError::getHandler (void *&data) const [protected], [virtual] This is just a convenience wrapper around the getHandlerCallback() and getHandlerData() methods. Reimplemented from SoError. Author Generated automatically by Doxygen for Coin from the source code. Version 3.1.3 Wed May 23 2012 SoMemoryError(3)
All times are GMT -4. The time now is 01:21 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy