Another question: class static functions and vbls

Another question: class static functions and vbls

Post by patri » Sun, 04 Nov 2001 00:53:15



One more question..  :)

Consider again my example class, say I had:

class Class {
        public:
                static Class foo();
                static const Class& constFoo();

Quote:}

and in foo:

static Class
Class::foo
{
        Class myvar;
        ...
        return myvar;

Quote:}

Is this *legal*?  Or would myvar also have to be declared static?
I'm assuming it is *not* because wouldn't myvar be placed on the runtime
stack, and then when the function ends, all of its contents from the
runtime stack are deleted?  Or is this a special case because foo
itself is static, so it will not necessarily run on the runtime stack?

And what about:

static const Class &
Class::constFoo() {
        Class myconstvar;
        ...
        return myconstvar;

Quote:}

Will I be able to return a pointer to a private variable of a static
class?  

Tks,
Patrick

--

http://www.tendim.cjb.net/
Like anime?   Got hotline?
hotline://twoaday.cjb.net/

 
 
 

Another question: class static functions and vbls

Post by Victor Bazaro » Sun, 04 Nov 2001 01:40:37



> One more question..  :)

> Consider again my example class, say I had:

> class Class {
> public:
> static Class foo();
> static const Class& constFoo();
> }

> and in foo:

> static Class

Lose the 'static' keyword here.  THAT'S illegal.

Quote:> Class::foo
> {
> Class myvar;
> ...
> return myvar;
> }

> Is this *legal*?  Or would myvar also have to be declared static?

No.  That's fine.  You're returning an object which will be created
when the function is about to return control to its caller.

Quote:> I'm assuming it is *not* because wouldn't myvar be placed on the runtime
> stack, and then when the function ends, all of its contents from the
> runtime stack are deleted?  Or is this a special case because foo
> itself is static, so it will not necessarily run on the runtime stack?

No, it has nothing to do with Class::foo being static.  It is so
called "returning by value".  Read about it in your favourite C++
book.

Quote:

> And what about:

> static const Class &

Again, lose the 'static' keyword here.

Quote:> Class::constFoo() {
> Class myconstvar;
> ...
> return myconstvar;
> }

> Will I be able to return a pointer to a private variable of a static
> class?

First, that's not a pointer, that's a _reference_.  And, no, you
will not be able.  Read FAQ, section 8.  Or your favourite C++
book.  They usually talk about returning a reference to a local
variable.

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

Another question: class static functions and vbls

Post by patri » Sun, 04 Nov 2001 11:53:12




> class Class {
> public:
> static Class foo();
> static const Class& constFoo();
> }

> and in foo:

> static Class

>Lose the 'static' keyword here.  THAT'S illegal.

Why is it illegal here?  (I've just written some code where I have
a static functon in a class definitoin (in the .h file) and it
compiles fine).

Thanks,
Patrick

--

http://www.tendim.cjb.net/
Like anime?   Got hotline?
hotline://twoaday.cjb.net/

 
 
 

Another question: class static functions and vbls

Post by Victor Bazaro » Sun, 04 Nov 2001 13:02:28





> > class Class {
> > public:
> > static Class foo();
> > static const Class& constFoo();
> > }

> > and in foo:

> > static Class

> >Lose the 'static' keyword here.  THAT'S illegal.

> Why is it illegal here?  (I've just written some code where I have
> a static functon in a class definitoin (in the .h file) and it
> compiles fine).

class HasStaticMember
{
    static void foo();  // declaration of a static member - OK

Quote:};

static void HasStaticMember::foo() // definition -- not OK
{

Quote:}

You had the definition outside the class.  That's why I said
"lose the static keyword".

Victor
--
Please remove capital A's from my address when replying by mail

 
 
 

Another question: class static functions and vbls

Post by patri » Mon, 05 Nov 2001 11:43:11


Victor,



>class HasStaticMember
>{
>    static void foo();  // declaration of a static member - OK
>};

>static void HasStaticMember::foo() // definition -- not OK
>{
>}

>You had the definition outside the class.  That's why I said
>"lose the static keyword".

Ahh, I see.  Yes, found the error in that.  Now, looking for
solutions.  Pls see my other news article.

Thanks for your help so far,
Patrick.

--

http://www.tendim.cjb.net/
Like anime?   Got hotline?
hotline://twoaday.cjb.net/

 
 
 

1. Invoking non-static functions from a static function

The C++ standard does not allow the invocation of non-static member
functions from a static member function. But look at this piece of
code.

class Y{
public:
        void fooY(){ cout << "Y::fooY()" << endl;}
        static void (Y::*pfooY)();

void (Y::*Y::pfooY)() = &Y::fooY;

int main(){

        Y y;
        (y.*Y::pfooY)();
        return 0;

Here, though the function pointer pfooY is static, by using the .*, we
are able to make it invoke a non-static member function of the same
class.

Would it not be simpler if the C++ standard allowed the invocation of
static member functions by instances and did the "binding"
automatically ? So, if a static member function was trying to access a
non-static member data, it would be ok if the function was being
invoked via an instance, else it will be a runtime error or an
exception.

I may not be making myself clear, but the rough idea is to have a
function that could behave as a static function as well as a
non-static function depending on how it was invoked ?

Regards,
-Vinayak


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. Tool to manage repackaging in context of CVS

3. static class member function

4. ToolAction gadget (Toolbox)

5. Static member functions and inheritance? DDE class implementation

6. good books for ocx on ce

7. Q: lifetime of static var in class member function

8. Can a null pointer to a class access a non-static member function?

9. excplicit template instantion, class with static function member

10. @GoW : exception-safe class with static members/functions ?

11. Error resolving a static member function in a template class

12. Using static class member functions in callbacks