Solved

return type of a function ?

Posted on 2003-10-27
4
171 Views
Last Modified: 2010-04-02
To C++ Experts,
    We can return a reference, a point, or a copy of the object from a function. The object can also be const. Is it illegal to return a static object ?
If so, when is it used ?
    i.e.
    class A{} ;
    const A* func(); //ok (right ?)
    static A* func(); // is this allowed ? if so, when do we use it ?
   
Thanks a lot !
   
0
Comment
Question by:meow00
4 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 9630848
> static A* func(); // is this allowed ? if so, when do we use it ?

This doesn't necessarily return a pointer to a static object. There is no way of indicating whether the object pointed to is in static memory or in the free store. There is nothing wrong with returning a pointer to a static object.

Having this declared outside a class is deprecated use of the keyword static. It is equivalent to putting a function into an unnamed namespace making it visible only to the module in which it is defined.

 i.e.
 
 namespace {
 A* func();
 }

Having this declared within a class means that the function may be called without instantiating the class, because the function does not use any instance variables.

 i.e.

 class X {...};
 class foo {
 ....
 static X* func(); /* Returns a pointer to an instance of X, but doesn't make use of any of the non-static class members of foo in doing so */
 };
0
 
LVL 1

Accepted Solution

by:
beavis_shenzhen earned 20 total points
ID: 9630972
static A* func();
the keyword "static" is for func() , not for "A*".
0
 

Assisted Solution

by:gdean
gdean earned 20 total points
ID: 9631146
class A
{
public:
     class A{} ;
    static A* func(){return Aptr;};
private:
     static A* Aptr;
};

A* A::Aptr=NULL;

This is an example of returning a static data member,  as beavis said statis refers to the function not the return type.   Statis simply means that it exists at runtime.  In this example you could invoke the function as A::func(), without having created an A object.  Also it may be worth noting that, any static data members in the class will be shared between all the objects of the class.  The most common example is a counter of objects.

class webpages
{
public:
     webpages(){++num;};
     ~webpages(){--num;};
     static unsigned long getNum(){return num;};
private:
     static unsigned long num;
};

unsigned long webpages::num=0;

/////////////////////////////////////////////////////////////////////////////////
if you create say 10 webpage objects in your program,  webpages::getNum() will return 10.  if you then destroy 5 of them webpages::getNum() will return 5.


Im not sure if this is what you were asking, but it sounded like you were a little bit confused about what exactly static means.  Hope this clears some things up.

0
 
LVL 1

Author Comment

by:meow00
ID: 9736889
Thanks for the answers and sorry for the late response, as I was out of town for a while.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question