Topic: struct member functions


Author: rkdious@ix.netcom.com (Daniel)
Date: 1996/07/19
Raw View
//---------------------------------------------------------------------------
typedef struct sample {

  void sampleData( const char* c ){ strcpy( sampleData, c ); };

  char sampleData[ 10 ];

} TSample;
//----------------------------------------------------------------------------

The above code would not compile for me.  The compiler (BC5) reported that
'sampleData( const char* ) was already defined'?

Can I not have a member function AND a member variable of the same name in a
structor?  I got around the problem by simply renaming the function to
'GetsampleData' but I'm still interested in knowing why this didn't work.

-Daniel
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Aftab Khan <mak@leicester.ac.uk>
Date: 1996/07/19
Raw View
Daniel wrote:
>
> //---------------------------------------------------------------------------
> typedef struct sample {
>
>   void sampleData( const char* c ){ strcpy( sampleData, c ); };
>
>   char sampleData[ 10 ];
>
> } TSample;
> //----------------------------------------------------------------------------
>
> The above code would not compile for me.  The compiler (BC5) reported that
> 'sampleData( const char* ) was already defined'?
>
> Can I not have a member function AND a member variable of the same name in a
> structor?  I got around the problem by simply renaming the function to
> 'GetsampleData' but I'm still interested in knowing why this didn't work.
>
> -Daniel
> ---
Because
{
  TSample aTSample;
  aTSample.someData;
}
is ambiguous.
Is the someData a char array, or a function pointer?
Besides, you'd get some very strange error messages from your compiler
when it came to function calling (at least, I would).
The fact that a name of a function equates to a function pointer in C is
very confusing, at times annoying.
You must have done this more than once:
if (bool_returning_function) ...
Oops! The address of 'bool_returning_function' isn't going to be 0, is
it?

Regards,
 Michael Hudson

Please don't email this address - it's not mine.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Paul J. Lucas" <grumpy@wizard.arc.nasa.gov>
Date: 1996/07/19
Raw View
In <4smu1i$inj@dfw-ixnews4.ix.netcom.com> rkdious@ix.netcom.com (Daniel)
writes:

>Can I not have a member function AND a member variable of the same name in a
>structor?

 You can not reuse any identifier in the same scope with the
 exception of overloaded functions.

 - Paul J. Lucas
   NASA Ames Research Center  Caelum Research Corporation
   Moffett Field, California  San Jose, California
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]