3
//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) ;
}
3
  • Because It's inside the same class ? It looks strange to me .
    – Ahmed
    Aug 28, 2010 at 11:14
  • 1
    Are you asking purely about passing a node by value as an argument or would you also find passing node* and node& strange?
    – Troubadour
    Aug 28, 2010 at 11:25
  • passing node* or node& is passing an address (I think) , but passing node by value is like copying a variable type inside a class which defines this type .
    – Ahmed
    Aug 28, 2010 at 11:31

5 Answers 5

5

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.

3
  • You could write copy constructors by taking a const node & instead of a plain node. Aug 28, 2010 at 11:43
  • A copy constructor even can't take the argument by value!
    – UncleBens
    Aug 28, 2010 at 12:04
  • True, I was alluding to the fact that you can use the type within its class/struct definition :) Aug 28, 2010 at 12:10
4

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).

1
  • Now , I understood why it's possible . Good Answer .
    – Ahmed
    Aug 28, 2010 at 12:16
2

yes, you can. its a valid case.

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

string& append( const string& str );
3
  • 2
    That's not in the C++ standard libraries... :S What a horrible function. Aug 28, 2010 at 11:22
  • that's still not a plain type, that's a reference. Aug 28, 2010 at 13:29
  • @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. Aug 28, 2010 at 17:42
0

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

class Apple {  
public:  
    Apple();  
    Apple(const Apple&);  
};  
0

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."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.