Solved

return type of a function ?

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

627 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