Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Class wide object declaration question

Reply
Thread Tools

Class wide object declaration question

 
 
jeff_j_dunlap@yahoo.com
Guest
Posts: n/a
 
      05-15-2007
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:bj 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

 
Reply With Quote
 
 
 
 
anon
Guest
Posts: n/a
 
      05-15-2007
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:bj 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:bj(arguments to myObject constructor);
 
Reply With Quote
 
 
 
 
Lionel B
Guest
Posts: n/a
 
      05-15-2007
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:bj 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:bj in some compilation unit:

myObject* myClass:bj;

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

--
Lionel B
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      05-15-2007
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:bj 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

 
Reply With Quote
 
Lionel B
Guest
Posts: n/a
 
      05-15-2007
On Tue, 15 May 2007 10:47:52 +0200, anon wrote:

> 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:bj 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:bj(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
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      05-15-2007
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:bj 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 ?

 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      05-15-2007
Lionel B wrote:
> On Tue, 15 May 2007 10:47:52 +0200, anon wrote:
>
>> 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:bj 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:bj(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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wide Screen not wide enough? michelebargeman@yahoo.com DVD Video 31 04-27-2006 08:50 PM
How to find at Runtime, if Created class object is instance of given class declaration Ami C++ 3 02-27-2006 04:59 PM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
char 8bit wide or 7bit wide in c++? Web Developer C++ 2 07-31-2003 08:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57