473,239 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 software developers and data experts.

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(const Foo& foo)
{
name_m = foo.Name();
}

string Foo::Name() const
{
return name_m;
}
void Foo::Name(string 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(const Bar& bar)
{
name_m = bar.Name();
}

string Bar::Name() const
{
return name_m;
}
void Bar::Name(string 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::operator Foo& () const
{
return *foo_m;
}

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

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

FooBar::operator 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(foo);
fooBar.SetBar(bar);

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

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

return 0;
}
.

Nov 16 '05 #1
0 1656

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

Similar topics

6
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
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
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...
6
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...
0
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...
9
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
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...
2
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
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.