Solved

return type of a function ?

Posted on 2003-10-27
4
167 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now