Topic: Public const feature?
Author: Antonio Vieiro Varela <favieiro@uscmail.usc.es>
Date: 1997/06/11 Raw View
Hi, all,
I am developping C++ scientific code for my Ph. D. The fact
is that I am worried about speed, so I am trying to avoid
the use of inline functions for accessing class members.
So I am substituting the usual code:
class AClass
{
private: int amember;
(...)
public: AClass( int X ):amember(x) {};
public: int getamember() { return amember; };
};
by something like:
class AClass
{
private: int amember;
public: const int &getamember;
public: AClass( int X ):amember(x),getamember(amember) {};
};
So after this I can access 'Aclass.getamember' and read
the 'amember' member without a function-call-penalty. I can
also change 'amember' and change 'getamember' accordingly.
The problem is that I am using twice as memory, so I would
like to propose the inclusion of a "public-const" feature.
This could consist of a declaration of a public-member which
could only be changed from inside a class' member functions.
It could be something like this
class AClass
{
public: "public-const" int amember;
public: AClass( int X ):amember(X) {};
public: void change(int X) {amember=X;}; // Ok. Changing
// from inside class
};
...
AClass A(5);
cout << A.amember << endl;
A.amember = 5; // Error, amember is const for public use
Or maybe there's a more elegant way to do this?
Thanks,
Antonio Vieiro
Applied Physics Dpt.
University of Santiago
SPAIN
[ 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
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
]
---
[ 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: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/06/12 Raw View
Antonio Vieiro Varela <favieiro@uscmail.usc.es> writes:
>I am developping C++ scientific code for my Ph. D. The fact
>is that I am worried about speed, so I am trying to avoid
>the use of inline functions for accessing class members.
This is a bad idea. Why are you worried about the speed
of inline functions? If inline functions aren't actually
inlined by your compiler, then you should get yourself a
decent compiler, rather than modifying your code.
>class AClass
>{
> private: int amember;
> public: const int &getamember;
> public: AClass( int X ):amember(x),getamember(amember) {};
>};
>
>So after this I can access 'Aclass.getamember' and read
>the 'amember' member without a function-call-penalty.
As well as the space cost that you noticed, you also pay the cost
of an extra level of indirection. This code is likely
to be less efficient than the version using inline functions.
>... so I would like to propose the inclusion of a "public-const" feature.
I don't think this buys you anything over inline functions.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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: ken@digitas.harvard.edu (Ken Shan)
Date: 1997/06/12 Raw View
Antonio Vieiro Varela (favieiro@uscmail.usc.es) wrote:
> Or maybe there's a more elegant way to do this?
Yes; it's called inline member functions. (:
Any more than slightly decent compiler nowadays should be able to
inline simple accessor member functions quite nicely for you.
--
Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
Party at Ken's sig! Everybody's invited!
---
[ 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: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/06/13 Raw View
Antonio Vieiro Varela <favieiro@uscmail.usc.es> wrote:
: I am developping C++ scientific code for my Ph. D. The fact
: is that I am worried about speed, so I am trying to avoid
: the use of inline functions for accessing class members.
: So I am substituting the usual code:
: class AClass
: {
: private: int amember;
: (...)
: public: AClass( int X ):amember(x) {};
: public: int getamember() { return amember; };
: };
No reasonable compiler will cause a performance difference between
reading amember directly or through getamember(), if the optimization
is turned on.
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ 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 ]