473,386 Members | 1,828 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,386 software developers and data experts.

Class wide object declaration question

Hello,

Whenever I need class wide access to an object, I declare it
dynamically:

class myClass
{
...
myObject* obj; // declared dynamically
...
Then I usually create an instance of the object within the
constructor:

myClass::myClass()
{
...
string str = "whatever";
obj = new myObj(str); // parameter passed to obj
...
Doing this, I am able to access myObj::obj anywhere from within my
class. Is it possible to declare obj as a static member variable? If
so, I have not been able to figure out how to do it.

Thanks in Advance

May 15 '07 #1
6 2019
je***********@yahoo.com wrote:
Hello,

Whenever I need class wide access to an object, I declare it
What is "class wide access to an object"?
dynamically:

class myClass
{
...
myObject* obj; // declared dynamically
...
Then I usually create an instance of the object within the
constructor:

myClass::myClass()
{
...
string str = "whatever";
obj = new myObj(str); // parameter passed to obj
...
Doing this, I am able to access myObj::obj anywhere from within my
What is myObj?
class. Is it possible to declare obj as a static member variable? If
so, I have not been able to figure out how to do it.
I do not see why not. You can do it like this:
class myClass
{
public:
static myObject obj;
};

myObject myClass::obj(arguments to myObject constructor);
May 15 '07 #2
On Tue, 15 May 2007 01:27:19 -0700, jeff_j_dunlap wrote:
Hello,

Whenever I need class wide access to an object, I declare it
dynamically:

class myClass
{
...
myObject* obj; // declared dynamically ...
Then I usually create an instance of the object within the constructor:

myClass::myClass()
{
...
string str = "whatever";
obj = new myObj(str); // parameter passed to obj ...
Doing this, I am able to access myObj::obj anywhere from within my
class. Is it possible to declare obj as a static member variable? If
so, I have not been able to figure out how to do it.
class myClass
{
...
static myObject* obj;
...
};

Note that this is just a declaration; you will also have to *define*
myClass::obj in some compilation unit:

myObject* myClass::obj;

myClass::obj will be accessible to all class member functions, including
static ones. Where/how you allocate/deallocate myClass::obj is up to you,
depending on what you are trying to do.

--
Lionel B
May 15 '07 #3
On May 15, 4:27 am, jeff_j_dun...@yahoo.com wrote:
Hello,

Whenever I need class wide access to an object, I declare it
dynamically:

class myClass
{
...
myObject* obj; // declared dynamically
...

Then I usually create an instance of the object within the
constructor:

myClass::myClass()
{
...
string str = "whatever";
obj = new myObj(str); // parameter passed to obj
...

Doing this, I am able to access myObj::obj anywhere from within my
class. Is it possible to declare obj as a static member variable? If
so, I have not been able to figure out how to do it.

Thanks in Advance
I'ld suggest reading the FAQ

[10.10] Why can't I initialize my static member data in my
constructor's initialization list?
[10.11] Why are classes with static data members getting linker
errors?
http://www.parashift.com/c++-faq-lit...html#faq-10.10

May 15 '07 #4
On Tue, 15 May 2007 10:47:52 +0200, anon wrote:
je***********@yahoo.com wrote:
>Hello,

Whenever I need class wide access to an object, I declare it

What is "class wide access to an object"?
>dynamically:

class myClass
{
...
myObject* obj; // declared dynamically ...
Then I usually create an instance of the object within the constructor:

myClass::myClass()
{
...
string str = "whatever";
obj = new myObj(str); // parameter passed to obj ...
Doing this, I am able to access myObj::obj anywhere from within my

What is myObj?
>class. Is it possible to declare obj as a static member variable? If
so, I have not been able to figure out how to do it.

I do not see why not. You can do it like this: class myClass
{
public:
static myObject obj;
};

myObject myClass::obj(arguments to myObject constructor);
Couple of points: I agree it is not clear what the OP is trying to
achieve, but (1) why make obj public, since the OP only seems to require
"class wide access" (which I take as meaning access for class member/
static functions)? and (2) in the OP's code obj was a *pointer* to a
myObject ... not sure why, but maybe there is a valid reason (which he
hasn't told us).

--
Lionel B
May 15 '07 #5
On May 15, 6:27 pm, jeff_j_dun...@yahoo.com wrote:
Hello,

Whenever I need class wide access to an object, I declare it
dynamically:

class myClass
{
...
myObject* obj; // declared dynamically
...

Then I usually create an instance of the object within the
constructor:

myClass::myClass()
{
...
string str = "whatever";
obj = new myObj(str); // parameter passed to obj
...

Doing this, I am able to access myObj::obj anywhere from within my
class. Is it possible to declare obj as a static member variable? If
so, I have not been able to figure out how to do it.
When you say "statically", what do you mean. If you mean the old
(somewhat deprecated) meaning of local to the compilation unit, then
you can't do that for a class. If you mean that there is one instance
per class, then that's the meaning of static in a class/struct.

I think this code below covers all the sorts of "storage class" that
exist.

static int s;

namespace { int y; /* anonymous namespace for y */}

// for most purposes, s and y are the same storage class - i.e.
visible in this
// compilation unit only.

struct A
{
int a; // one a per instance of A
static int b; // one b - ever
const static int c = 5; // only allowed to do this for integral
types.

A * instance()
{
int f; // created every time execution passes this point.
(auto)
static A v; // only one v in the entire program
// and only created once - the first time
through.
return &v;
}
};

int A::b = 2; // need to define b somewhere. usually in one place only

// special ones - these may show up in the new revision of the
standard

void f()
{
register int i; // this is like auto but you can't take the address
of i
// which allows the compiler to do optimizations
for i
}

__thread int t; // one instance of t per thread.

.... did I miss one ?

May 15 '07 #6
Lionel B wrote:
On Tue, 15 May 2007 10:47:52 +0200, anon wrote:
>je***********@yahoo.com wrote:
>>Whenever I need class wide access to an object, I declare it
What is "class wide access to an object"?
>>dynamically:

class myClass
{
...
myObject* obj; // declared dynamically ...
Then I usually create an instance of the object within the constructor:

myClass::myClass()
{
...
string str = "whatever";
obj = new myObj(str); // parameter passed to obj ...
Doing this, I am able to access myObj::obj anywhere from within my
What is myObj?
>>class. Is it possible to declare obj as a static member variable? If
so, I have not been able to figure out how to do it.
I do not see why not. You can do it like this: class myClass
{
public:
static myObject obj;
};

myObject myClass::obj(arguments to myObject constructor);

Couple of points: I agree it is not clear what the OP is trying to
achieve, but (1) why make obj public, since the OP only seems to require
"class wide access" (which I take as meaning access for class member/
static functions)? and (2) in the OP's code obj was a *pointer* to a
myObject ... not sure why, but maybe there is a valid reason (which he
hasn't told us).
Salt_Peter gave much better response then both of us did ;)

Anyway, that was just a simple example, therefore you can put it to
private, make it a pointer, reference or whatever you like.
May 15 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
10
by: William Stacey | last post by:
I know the following is not allowed, but shouldn't it be? sharedObject is part of Derived and should be able to be set in the constructor - no? tia public abstract class Base1 { protected...
6
by: Carlos | last post by:
Hi all, I am trying to access a public field of another form class within the same namespace. The field is public, what is the best way to access it from a different class? I defined as private...
13
by: cgough | last post by:
My true programming language is C++. I am at best a VB6 hacker that is just getting into VB.NET. I have a quick question about when to new and when not to new. Consider the following 2 classes....
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
4
by: saneman | last post by:
In the main below I create a Swat class in three ways: class Swat { public: Swat(){ n = 0; } Swat(int a){ n = a;
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.