473,382 Members | 1,639 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,382 software developers and data experts.

overloading .

Hi folks,

I have a smart pointer class that guarantees the existence of a valid
pointee. Hence it does not really feel like a pointer (e.g., it would be
pointless to use it as a next-pointer in a list or tree structure). So I
would like to remove the pointer-likeness from the class interface. Thus, I
am wondering if it is possible to make a smart pointer that does not look
like a pointer. Here is a basic idea:

#include <iostream>

template < typename T >
class dummy {
private:

T * ptr;

public:

dummy ( T const & t = T() )
: ptr ( new T ( t ) )
{}

operator T & ( void ) {
return( *ptr );
}

operator T const & ( void ) const {
return( *ptr );
}

}; // named_class

struct T {

int k;

};

int main ( void ) {
dummy< int > i ( 5 );
i = 6; // works
std::cout << i << '\n'; // works
dummy<T> t;
t.k = 5; // COMPILE TIME ERROR
}

I would like to define the template dummy<> in such a way that this
compiles and delivers the expected results, i.e., I would like to overload
"operator." if there was such a thing. Any ideas as to how this could be
done (or any ideas how to prove it impossible) would be appreciated.
Thanks

Kai-Uwe Bux
Jul 23 '05 #1
5 1266
Kai-Uwe Bux wrote:
Hi folks,

I have a smart pointer class that guarantees the existence of a valid
pointee. Hence it does not really feel like a pointer (e.g., it would be
pointless to use it as a next-pointer in a list or tree structure). So I
would like to remove the pointer-likeness from the class interface. Thus,
I am wondering if it is possible to make a smart pointer that does not
look like a pointer. Here is a basic idea:
snip
int main ( void ) {
dummy< int > i ( 5 );
i = 6; // works
std::cout << i << '\n'; // works
dummy<T> t;
t.k = 5; // COMPILE TIME ERROR
}

I would like to define the template dummy<> in such a way that this
compiles and delivers the expected results, i.e., I would like to overload
"operator." if there was such a thing. Any ideas as to how this could be
done (or any ideas how to prove it impossible) would be appreciated.


This doesn't answer your questions, but it looks like you need to overload
the operator*, so the line in question would look like:

(*t).k = 5;

I don't think there is such a thing as operator.()

--
Alvin
Jul 23 '05 #2
Alvin wrote:
[...]
I don't think there is such a thing as operator.()


There is. It's not overloadable. There are three other operators that
are listed as non-overloadable: member access through pointer to member
for objects (.*), scope resolution (::) and ternary (?:). Of course,
'sizeof' is not overloadable either.

V
Jul 23 '05 #3
Victor Bazarov wrote:
Alvin wrote:
[...]
I don't think there is such a thing as operator.()


There is. It's not overloadable. There are three other operators that
are listed as non-overloadable: member access through pointer to member
for objects (.*), scope resolution (::) and ternary (?:). Of course,
'sizeof' is not overloadable either.


Thanks, I found the clause in the standard that says operator. cannot be
overloaded. Too bad.

Anyway, do you know of a work-around that would solve my original problem
of making dummy<T> behave like it provided the same interface as T although
all actual calls would be forwarded to a pointee of type T?
Best

Kai-Uwe Bux
Jul 23 '05 #4
Kai-Uwe Bux wrote:
Victor Bazarov wrote:

Alvin wrote:
[...]
I don't think there is such a thing as operator.()


There is. It's not overloadable. There are three other operators that
are listed as non-overloadable: member access through pointer to member
for objects (.*), scope resolution (::) and ternary (?:). Of course,
'sizeof' is not overloadable either.

Thanks, I found the clause in the standard that says operator. cannot be
overloaded. Too bad.

Anyway, do you know of a work-around that would solve my original problem
of making dummy<T> behave like it provided the same interface as T although
all actual calls would be forwarded to a pointee of type T?


Nothing elegant, I'm afraid. I once wrote a "reference" class (similar to
what you're doing), and, IIRC, used the unary + operator to return the
reference to the object, which in your terms would look like

dummy<T> t;
(+t).k = 5;

(Not much better than (*t).k, if you ask me)

I'd say, if you need things done, use a smart_ptr or shared_ptr, and don't
bother with the operator dot.

V
Jul 23 '05 #5
Victor Bazarov wrote:
Kai-Uwe Bux wrote:
Victor Bazarov wrote: [snip] Anyway, do you know of a work-around that would solve my original problem
of making dummy<T> behave like it provided the same interface as T
although all actual calls would be forwarded to a pointee of type T?


Nothing elegant, I'm afraid. I once wrote a "reference" class (similar to
what you're doing), and, IIRC, used the unary + operator to return the
reference to the object, which in your terms would look like

dummy<T> t;
(+t).k = 5;

(Not much better than (*t).k, if you ask me)

I'd say, if you need things done, use a smart_ptr or shared_ptr, and don't
bother with the operator dot.


Thanks again. I will go with the pointer-like syntax for the time being.
Although it feels somewhat odd that C++ offers all kinds of nice syntactic
suggar and yet does not seem to have a way to fake inheritance or to
forward in interface.
Best

Kai-Uwe Bux
Jul 23 '05 #6

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

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
31
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
11
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.