Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

private Helper functions in C++

1,079 views
Skip to first unread message

Hao Zhang

unread,
Jun 20, 2010, 1:13:24 AM6/20/10
to
I'm a bit unclear about private functions in C++.

If a class is defined in a header file with the appropriate signatures,
is it possible to add private helper functions in the cpp file that were
not defined in the header file?

If not, are we supposed to add any helper functions as global functions?


Thanks

Roman Lapin

unread,
Jun 20, 2010, 3:25:28 AM6/20/10
to

You cannot add helper functions that will be able to see private
sections of the class without modifying the header file.
So far I did not see a particular need for helpers (sure, the - and *
are a bit long but there isn't much you can outsource).

Hao Zhang

unread,
Jun 20, 2010, 11:08:00 AM6/20/10
to

Thanks.


So in general, once the header file for a c++ class is defined, the
functions for the class are set in stone?

ie..

Foo.h:


class Foo {
public:
bar();
private:
baz();

}

There is no way to add any private functions in Foo.cpp that are not
declared in the header file?

Roman Lapin

unread,
Jun 20, 2010, 12:42:43 PM6/20/10
to

Nope, no way that I know of anyways.

Caroline Kierstead

unread,
Jun 20, 2010, 4:59:44 PM6/20/10
to
In article <hvlge4$osj$1...@rumours.uwaterloo.ca>,
Roman Lapin <cs...@student.cs.uwaterloo.ca> wrote:

>Hao Zhang wrote:
>>
>> So in general, once the header file for a c++ class is defined, the
>> functions for the class are set in stone?

Yes, but you *can* add free routines to the .cc file that will not be
visible outside.

>> ie..
>>
>> Foo.h:
>>
>>
>> class Foo {
>> public:
>> bar();
>> private:
>> baz();
>>
>> }

For example:

Foo.cc

void helper() {...}
void Foo::bar() { helper; }
...

>> There is no way to add any private functions in Foo.cpp that are not
>> declared in the header file?
>
>Nope, no way that I know of anyways.

Not if you want them to be part of the class, no.
--
--
Caroline Kierstead, Undergraduate Operations Coordinator
David R. Cheriton School of Computer Science
University of Waterloo, DC3122 (519) 888-4567 x36226

0 new messages