cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Returning self as a property Costum Clas
Returning self as a property Costum Class
May 29, 2011 at 8:28pm UTC
FranciscoDias
(9)
is this possible.
(Pseudo-code)
class myclass
{
int a;
int b;
}
...
myclass a;
a.a = 13;
a.b = 21;
printf("%d",a);
(I wanted it to print "13", it means that self value is equal to 'a' property!)
Is this possible?? Thank you in advance!!
May 29, 2011 at 8:35pm UTC
firedraco
(6241)
Not with printf(). With C++ and iostreams you can do this however.
May 29, 2011 at 8:40pm UTC
FranciscoDias
(9)
How?
cout << a;
will this print, a.a ?!
May 29, 2011 at 8:50pm UTC
Disch
(13742)
if you properly overload the << operator, yes:
1
2
3
4
5
std::ostream&
operator
<< (std::ostream& o,
const
myclass& a) { o << a.a;
return
o; }
May 29, 2011 at 10:46pm UTC
LBEaston
(91)
If you want to use it often in more than just cout and printf you might want to write a myclass::getSelf() function that returns 'a' (the member variable)
Topic archived. No new replies allowed.