473,836 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const& typecast

After more searching I found that defining const operators
can cause problems with many compilers due to the way it
interprets the C++ standard. I removed the const
operators from my class and it seems to be working fine
now.
-----Original Message-----
I have C++ code that has been running just fine in VS6 fora couple of years. I brought it into VS.NET with managed
extensions turned off and it compiles/links just fine,
however I get access violations at runtime. The code in
question is basically a smart pointer which has the type
cast operators overloaded. There is both a non-const and
const operator defined. The non-const type cast works,
the const type cast does not. I am including a simple
example that exhibits the same behavior. Thanks in
advance for looking at this. I'm stumped.
#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

//////////////////////////////////////////
//////////////////////////////////////////
class Foo
{
public:
Foo();
Foo(const Foo& foo);
string Name() const;
void Name(string name);
private:
string name_m;
};

Foo::Foo()
{
name_m = "";
}

Foo::Foo(con st Foo& foo)
{
name_m = foo.Name();
}

string Foo::Name() const
{
return name_m;
}
void Foo::Name(strin g name)
{
name_m = name;
}

//////////////////////////////////////////
//////////////////////////////////////////
class Bar
{
public:
Bar();
Bar(const Bar& bar);
string Name() const;
void Name(string name);
private:
string name_m;
};

Bar::Bar()
{
name_m = "";
}

Bar::Bar(con st Bar& bar)
{
name_m = bar.Name();
}

string Bar::Name() const
{
return name_m;
}
void Bar::Name(strin g name)
{
name_m = name;
}

//////////////////////////////////////////
//////////////////////////////////////////
class FooBar
{
public:
FooBar();
void SetFoo(Foo& foo);
void SetBar(Bar& bar);
operator Foo& () const;
operator const Foo& () const;
operator Bar& () const;
operator const Bar& () const;
private:
Foo* foo_m;
Bar* bar_m;
};

FooBar::FooBar ()
{
foo_m = NULL;
bar_m = NULL;
}

void FooBar::SetFoo( Foo& foo)
{
foo_m = new Foo(foo);
}

void FooBar::SetBar( Bar& bar)
{
bar_m = new Bar(bar);
}

FooBar::operat or Foo& () const
{
return *foo_m;
}

FooBar::operat or const Foo& () const
{
return *foo_m;
}

FooBar::operat or Bar& () const
{
return *bar_m;
}

FooBar::operat or const Bar& () const
{
return *bar_m;
}

//////////////////////////////////////////
//////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
Foo foo;
Bar bar;
foo.Name("foo") ;
bar.Name("bar") ;

cout << "foo.Name() = " << foo.Name() << endl;
cout << "bar.Name() = " << bar.Name() << endl;

FooBar fooBar;
fooBar.SetFoo(f oo);
fooBar.SetBar(b ar);

cout << "((Foo&)fooBar) .Name() = " << ((Foo&)
fooBar).Name () << endl;
cout << "((Bar&)fooBar) .Name() = " << ((Bar&)
fooBar).Name () << endl;

cout << "((const Foo&)fooBar).Na me() = " << ((const
Foo&)fooBar).N ame() << endl;
cout << "((const Bar&)fooBar).Na me() = " << ((const
Bar&)fooBar).N ame() << endl;

return 0;
}
.

Nov 16 '05 #1
0 1711

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4370
by: hoox2 | last post by:
void push_front(Node const*& head, int data); Can someone tell me what it means for "const*&"? A reference or a pointer?
12
2627
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
25
2951
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default construction and then.. // some other processing and/or changing 'retval' return retval; }
6
15382
by: p|OtrEk | last post by:
What is practic difference between this two declarations? If i want call my func with func("blah") i could write: 1) func(std::string const& arg1) 2) func(const std::string& arg1) Whats better to use if i dont want to change content of arg1 it in func body? -- << pozdrawiam -lysek- @ irc.freenode.net#linux.com.pl << prompt$ :(){ :|:& };: << echo mail | sed 's/__NOSPAM//g'
0
471
by: tom olson | last post by:
See the following related link: http://gcc.gnu.org/ml/gcc-bugs/1999-10/msg00173.html >-----Original Message----- >I have C++ code that has been running just fine in VS6 for >a couple of years. I brought it into VS.NET with managed >extensions turned off and it compiles/links just fine, >however I get access violations at runtime. The code in
9
1859
by: miaohua1982 | last post by:
the program is as follows: #include <vector> using namespace std; class A{}; int main() { A* const &p = NULL; vector<A*B(3,NULL); //there is a compile error B.push_back(NULL);
13
3985
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code is correct according to the standard ?
2
2220
by: ek | last post by:
This first example does not work (cannot be overloaded): int& operator()(int a) { // (1) return a; } int const& operator()(int a) { // (2) return a; }
2
9160
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
0
9812
marktang
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...
0
9657
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,...
0
10532
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10577
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,...
0
10243
tracyyun
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...
0
5642
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...
0
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4443
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.