tags:

views:

102

answers:

5
//node.h 
class node 
{
      public:
            void sort(node n);

};

I didn't try the code yet . But It's interesting to know if is this a valid case and Why ?

Edit :

This leads me to another question : Can I declare FOO inside a member function like this ?

//FOO.h
Class FOO
{
   public:
   void sort(int n) ;
   void swap(int x , int y ); 
}

//FOO.cpp

void FOO::sort (int n)
{
     FOO obj;
     obj.swap(3 , 5) ;
}
+2  A: 

yes, you can. its a valid case.

you can find some sample in c++ standard libs :

string& append( const string& str );
mohammad shamsi
That's not in the C++ standard libraries... :S What a horrible function.
Peter Alexander
-1. The OP asked about plain types, not pointers.
Philip Potter
@philip, @Peter: thanks, sample edited.
mohammad shamsi
that's still not a plain type, that's a reference.
Philip Potter
hum, that's just a sample.
mohammad shamsi
@mohammed: It still doesn't reflect the question or come from the Library. A better example would be `size_t map::iterator::operator-( map::iterator )`, although the function isn't explicitly specified as such and is very likely to be implemented as a free (non-member) function.
Potatoswatter
+5  A: 

Yes, that's perfectly valid. If you couldn't do that, how could you write copy constructors?

A similar, but not valid case, is including the same type as a member of your class/struct.

struct Foo
{
  Foo m_foo;
};

You can't do that, because it's essentially a circular definition, and if you had:

struct Foo
{
  Foo m_foo;
  Foo m_bar;
};

Then a Foo would be 2 Foos, which would be 4 Foos, which would be 8 Foos etc. which makes no sense.

You can, on the other hand, have pointers to the same type:

struct Foo
{
  Foo* m_foo;
};

Because a pointer to Foo isn't the same as Foo. A pointer is a pointer, no matter what it points to, so there is no circular definition or dependencies.

Peter Alexander
+1 Good point on having yourself has a member.
Skurmedel
Philip Potter
A copy constructor even can't take the argument by value!
UncleBens
True, I was alluding to the fact that you can use the type within its class/struct definition :)
Peter Alexander
+3  A: 

Having now established that your question is solely related to passing node by value in a member (rather than passing node* or node&) the answer is still yes. You can even define the body of the member within the class is you so wish.

As to why, think of things from the compiler's point of view. As it parses the class all it really needs to know are what data members there are within it and what the function member signatures are. None of the function member definitions need to be parsed at this point so the compiler would not be alarmed at the prospect of seeing an actual node as an argument. Only when it's finished parsing the data and signatures would it go back and actually deal with the function member definitions and at that point it knows precisely what a node is.

In answer to your second question you can define instances of your class within the member (for the same reason).

Troubadour
Now , I understood why it's possible . Good Answer .
Ahmed
A: 

This is done quite often. A copy constructor is a a good example:

class Apple {  
public:  
    Apple();  
    Apple(const Apple&);  
};  
skimobear
A: 

The short answer is Yes:

The long answer is:

$9.2/2- "A class is considered a completely-defined object type (3.9) (or complete type) at the closing } of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments and constructor ctor-initializers (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification."

Chubsdad