Solved

return type of a function ?

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

816 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

10 Experts available now in Live!

Get 1:1 Help Now