Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

return type of a function ?

Posted on 2003-10-27
4
Medium Priority
?
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 80 total points
ID: 9630972
static A* func();
the keyword "static" is for func() , not for "A*".
0
 

Assisted Solution

by:gdean
gdean earned 80 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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

688 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