class Cow
{
public:
void EatGrass() const {}
};
class Brow
{
private:
Cow cow;
public:
operator Cow&()
{
return cow;
}
};
int main()
{
Brow k;
k.EatGrass();
}
Is there any way I can achieve the above, whereby a Brow is implicitly
converted to a Cow? Here would be another example:
class Blah
{
private:
double a;
public:
operator double&()
{
return a;
}
};
int main()
{
Blah jk;
jk = 52.0;
}
-JKop 14 1465
"JKop" <NU**@NULL.NULL > wrote in message
news:W1******** **********@news .indigo.ie... class Cow { public:
void EatGrass() const {} };
class Brow { private:
Cow cow;
public:
operator Cow&() { return cow; } };
int main() { Brow k;
k.EatGrass(); }
Is there any way I can achieve the above, whereby a Brow is implicitly converted to a Cow?
Yes:
class Brow
{
private:
Cow cow;
public:
Cow& operator. ()
{
return cow;
}
};
:-)
Seriously .. this is a reasona to prefer non-member functions to member
functions.
class Brow
{
private:
Cow cow;
public:
operator Cow&()
{
return cow;
}
operator const Cow&() const
{
return cow;
}
};
void EatGrass(const Cow&);
Jonathan
Jonathan Turkanis posted: "JKop" <NU**@NULL.NULL > wrote in message news:W1******** **********@news .indigo.ie... class Cow { public:
void EatGrass() const {} };
class Brow { private:
Cow cow;
public:
operator Cow&() { return cow; } };
int main() { Brow k;
k.EatGrass(); }
Is there any way I can achieve the above, whereby a Brow is implicitly converted to a Cow?
Yes:
class Brow { private:
Cow cow;
public:
Cow& operator. () { return cow; } };
:-)
By golly I love it!
So now we can do the following:
void SomeFunc(Cow& k);
int main()
{
Brow t;
SomeFunc(t); //operator Cow&
}
Plus we can access it's members:
int main()
{
Brow t;
t.EatGrass();
}
The only thing left is to access its operators:
int main()
{
Brow t;
t = 6;
//Assuming Cow has an assignment operator
}
Is there any easy way to do this, other than rhyming them all off:
class Brow
{
private:
Cow f;
public:
operator==(cons t Cow& r)
{
return f == r;
}
operator*(const Cow& r)
{
return f * r;
}
};
Templates would be required to get in all the operators and variable
argument types.
-JKop
For christ's sake it won't compile. You can't overload "."!
-JKop
Jonathan Turkanis wrote: Yes:
class Brow { private:
Cow cow;
public:
Cow& operator. () { return cow; } };
:-)
Seriously .. this is a reasona to prefer non-member functions to member functions.
class Brow { private:
Cow cow;
public:
operator Cow&() { return cow; }
operator const Cow&() const { return cow; } };
void EatGrass(const Cow&);
However constructors should be preferred when another class has to be
made compatible with the current class, and operator types() when our
current class has to become compatible with another class, of which we
have no access to its definition.
Regards,
Ioannis Vranos http://www23.brinkster.com/noicys
"Ioannis Vranos" <iv*@guesswh.at .grad.com> wrote in message
news:ch******** ***@ulysses.noc .ntua.gr... Jonathan Turkanis wrote:
Yes:
class Brow { private:
Cow cow;
public:
Cow& operator. () { return cow; } };
:-)
Seriously .. this is a reasona to prefer non-member functions to member functions.
class Brow { private:
Cow cow;
public:
operator Cow&() { return cow; }
operator const Cow&() const { return cow; } };
void EatGrass(const Cow&); However constructors should be preferred when another class has to be made compatible with the current class, and operator types() when our current class has to become compatible with another class, of which we have no access to its definition.
I was thinking of Cow as the class to which we didn't have access. We can still
define Brow as above, and
void EatGrass(const Cow& cow) { cow.EatGrass(); }
Jonathan
"Ioannis Vranos" <iv*@guesswh.at .grad.com> wrote in message
news:ch******** ***@ulysses.noc .ntua.gr... JKop wrote:
You can't overload "."!
Yes you can't. But I think the first one he provided was a joke.
Right. Maybe I should have used a different smiley: :-D ???
Jonathan
"JKop" <NU**@NULL.NULL > wrote in message
news:W1******** **********@news .indigo.ie... class Cow { public:
void EatGrass() const {} };
class Brow { private:
Cow cow;
public:
operator Cow&() { return cow; } };
int main() { Brow k;
k.EatGrass(); }
Is there any way I can achieve the above, whereby a Brow is implicitly converted to a Cow? Here would be another example:
class Blah { private:
double a;
public:
operator double&() { return a; } };
int main() { Blah jk;
jk = 52.0; }
-JKop
You could also use a ClassID approach like this :
( a bit more code than the other suggestions, but quite powerful)
typedef char ClassID;
class CClassIDBase
{
public:
static ClassID
m_cClassID;
virtual CClassIDBase* RequestClass(co nst ClassID& c)
{
return (&c == &m_cClassID) ? this : NULL;
}
};
class CCow : public CClassIDBase
{
public:
static ClassID
m_cClassID;
public:
void EatGrass(){}
virtual CClassIDBase* RequestClass(co nst ClassID& c)
{
return (&c == &m_cClassID) ? this : CClassIDBase::R equestClass(c);
}
};
class CBrow : CClassIDBase
{
public:
static ClassID
m_cClassID;
CCow
m_cCow;
public:
virtual CClassIDBase* RequestClass(co nst ClassID& c)
{
if (&c == &CCow::m_cClass ID)
{
return &m_cCow;
}
else
{
return (&c == &m_cClassID) ? this : CClassIDBase::R equestClass(c);
}
}
};
ClassID
CClassIDBase::m _cClassID = 0,
CCow::m_cClassI D = 0,
CBrow::m_cClass ID = 0;
int main(int argc, char* argv[])
{
CCow
*pcCow;
CBrow
cBrow;
if ((pcCow = (CCow*)cBrow.Re questClass(CCow ::m_cClassID)) != NULL)
{
pcCow->EatGrass();
}
return 0;
}
* JKop: class Cow { public:
void EatGrass() const {} };
class Brow { private:
Cow cow;
public:
operator Cow&() { return cow; } };
int main() { Brow k;
k.EatGrass(); }
Is there any way I can achieve the above, whereby a Brow is implicitly converted to a Cow?
Very simple, derive Brow from Cow:
class Brow: public Cow
{
};
Here would be another example:
class Blah { private:
double a;
public:
operator double&() { return a; } };
int main() { Blah jk;
jk = 52.0; }
Give class Blah an assignment operator (which implies also a copy
constructor) and perhaps also a conversion to double,
class Blah
{
public:
Blah( Blah const& another ) { ... }
Blah( double number ) { ... }
Blah& operator=( double number )
{
...
return *this;
}
operator double() const
{
return ...;
}
};
What you cannot do (except by providing access to Blah internals)
is to have assignments to something of 'double' type affect a Blah.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Joona I Palaste |
last post by:
AFAIK the C standard divides behaviour of different things into four
classes.
1) Defined behaviour. The implementation must do exactly what the
standard says.
2) Implementation-defined behaviour. The implementation must choose some
behaviour, document it, and be consistent about it.
3) Unspecified behaviour. The implementation must pick one of several
possible behaviours, but does not need to be consistent about it.
4) Undefined...
|
by: Nitin Bhardwaj |
last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a
student and have been doing & studying C programming for the last 4
years).....and also a regular reader of "comp.lang.c"
I don't have a copy of ANSI C89 standard,therefore i had to post this
question:
What is the difference between "unspecified" behaviour & "undefined"
behaviour of some C Code ??
|
by: RoSsIaCrIiLoIA |
last post by:
On Mon, 07 Feb 2005 21:28:30 GMT, Keith Thompson <kst-u@mib.org>
wrote:
>"Romeo Colacitti" <wwromeo@gmail.com> writes:
>> Chris Torek wrote:
>>> In article <4205BD5C.6DC8@mindspring.com>
>>> pete <pfiland@mindspring.com> wrote:
>
>>> >If you have
>>> > int array;
>>> >then
|
by: DeltaOne |
last post by:
#include<stdio.h>
typedef struct test{
int i;
int j;
}test;
main(){
test var;
var.i=10;
var.j=20;
|
by: |
last post by:
I have earlier used an HttpModule that did URL rewrites on the BeginRequest
event.
Now I am trying to use the same module in a different application on a new
and upgraded machine (winxp sp2).
The Module is registered via Web.config. The registration is OK. When asking
for an existing .aspx page, the eventhandler is called as it should.
HOWEVER - if the request url is for a non-existant file, I get a 404 - file
| |
by: Frederick Gotham |
last post by:
I have a general idea of the different kinds of behaviour described by the
C Standard, such as:
(1) Well-defined behaviour:
int a = 2, b = 3;
int c = a + b;
(Jist: The code will work perfectly.)
|
by: wizofaus |
last post by:
Hi,
I have a class for buttons which relies on IE's misinterpretation of
"overflow: visible" to set a minimum width - I.E. will automatically
horizontally expand any buttons whose text doesn't fit. This is
exactly the behaviour I want, but is non-standard, and Firefox simply
truncates the text (actually I thought it was supposed to spill outside
the button, but either way is useless).
I've tried using min-width under Firefox to get...
|
by: Sheth Raxit |
last post by:
Machine 1 :
bash-3.00$ uname -a
SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R
bash-3.00$ gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/
specs
gcc version 2.95.3 20010315 (release)
|
by: Franz Hose |
last post by:
the following program, when compiled with gcc and '-std=c99', gcc says
test.c:6: error: jump into scope of identifier with
variably modified type
that is, it does not even compile.
lcc-win32, on the other hand, reports
Warning test.c: 7 unreachable code
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |