Hi everybody:
I have implemented a Pointer class template that uses reference
counting to deallocate memory automatically.
I have the following code using my Pointer:
char* auxchar = new char[100];
strcpy(auxchar, "Hola mundo");
Pointer<char> charPointer = Pointer<char>(auxchar);
....
Is possible to implement an operator overloading or some stuff to do
something like this:
printf("Message: %s\n", charPointer);
that should return a <T*> to avoid create a method like
charPointer.getData() ?
Thanks in advance
Ernesto 8 1988
Ernesto wrote: Hi everybody:
I have implemented a Pointer class template that uses reference counting to deallocate memory automatically.
I have the following code using my Pointer:
char* auxchar = new char[100]; strcpy(auxchar, "Hola mundo");
Pointer<char> charPointer = Pointer<char>(auxchar);
...
Is possible to implement an operator overloading or some stuff to do something like this:
printf("Message: %s\n", charPointer);
that should return a <T*> to avoid create a method like charPointer.getData() ?
Basically yes, but not in conjunction with printf, because it is a function
with a variable argument list. Those functions are not type safe, meaning
that the compiler doesn't implicitly know that it has to convert your
Pointer<char> into a char*. You'd have to use a cast, like:
printf("Message: %s\n", static_cast<char*>(charPointer));
You can do that by implementing an operator char* for your class. Something
like:
template <typename T>
class Pointer
{
public:
operator T*()
{
return ptr_;
}
private:
T* ptr;
};
However, such a conversion operator is dangerous, since it could lead to
subtle errors, and it's usually better to stick with the getData() member
function.
Rolf Magnus schrieb: Ernesto wrote:
Hi everybody:
I have implemented a Pointer class template that uses reference counting to deallocate memory automatically.
I have the following code using my Pointer:
char* auxchar = new char[100]; strcpy(auxchar, "Hola mundo");
Pointer<char> charPointer = Pointer<char>(auxchar);
...
Is possible to implement an operator overloading or some stuff to do something like this:
printf("Message: %s\n", charPointer);
that should return a <T*> to avoid create a method like charPointer.getData() ?
Basically yes, but not in conjunction with printf, because it is a function with a variable argument list. Those functions are not type safe, meaning that the compiler doesn't implicitly know that it has to convert your Pointer<char> into a char*. You'd have to use a cast, like:
printf("Message: %s\n", static_cast<char*>(charPointer));
You can do that by implementing an operator char* for your class. Something like:
template <typename T> class Pointer { public: operator T*() { return ptr_; } private: T* ptr; };
However, such a conversion operator is dangerous, since it could lead to subtle errors, and it's usually better to stick with the getData() member function.
Just out of curiosity, would the overloaded char* cast operator work
better when using streams
(like cout << "Message: " << charPointer << endl) instead of printf()?
Oliver Gerlich
--
To reply by mail, please remove _block_ from address.
Oliver Gerlich wrote: Just out of curiosity, would the overloaded char* cast operator work better when using streams (like cout << "Message: " << charPointer << endl) instead of printf()?
Yes, although providing an operator<<(ostream &, yourclass &) overload
would be better.
--
Ioannis Vranos http://www23.brinkster.com/noicys
Hi:
Thanks a lot for your tips!
I am developing a library and I was thinking about implementing all my
classes using my Pointer class template, by example, using:
void Array::AddElement(Pointer<T> aElement);
Pointer<Array> Windows::GetComponents();
instead of
void Array::AddElement(T* aElement);
Array* Widget::GetInnerWidgets();
Do you think it is a good idea? Or should I use my reference count
Pointer internally (à la "copy on write" mechanism as in the string
classes) and provide classical pointers in my API ? If it is a good
idea, how should I handle the circular references?
Best regards
Ernesto Basically yes, but not in conjunction with printf, because it is a function with a variable argument list. Those functions are not type safe, meaning that the compiler doesn't implicitly know that it has to convert your Pointer<char> into a char*. You'd have to use a cast, like:
printf("Message: %s\n", static_cast<char*>(charPointer));
You can do that by implementing an operator char* for your class. Something like:
template <typename T> class Pointer { public: operator T*() { return ptr_; } private: T* ptr; };
However, such a conversion operator is dangerous, since it could lead to subtle errors, and it's usually better to stick with the getData() member function.
Ioannis Vranos wrote: Oliver Gerlich wrote:
Just out of curiosity, would the overloaded char* cast operator work better when using streams (like cout << "Message: " << charPointer << endl) instead of printf()?
Yes, although providing an operator<<(ostream &, yourclass &) overload would be better.
Rather an operator<<(ostream&, const yourclass&).
In article <f7*************************@posting.google.com> ,
Ernesto <eb*****@hotmail.com> wrote: Hi everybody:
I have implemented a Pointer class template that uses reference counting to deallocate memory automatically.
I have the following code using my Pointer:
char* auxchar = new char[100]; strcpy(auxchar, "Hola mundo");
Pointer<char> charPointer = Pointer<char>(auxchar);
...
Is possible to implement an operator overloading or some stuff to do something like this:
printf("Message: %s\n", charPointer);
that should return a <T*> to avoid create a method like charPointer.getData() ?
I'm curious of your tradeoffs of choosing doing this over just
using a std::string?
--
Greg Comeau / Comeau C++ 4.3.3, for C++03 core language support
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Hi:
I used a char* just as an example. I am actually implementing a
reference count smart pointer and I want it to be casted to the
specified template instantiation, id est, if I define:
Pointer<int> intPtr = new int[10];
int* otherPtr = intPtr;
or
Pointer<char> charPtr = new char[10];
char* otherCPtr = charPtr;
both should work.
:)
Best regards
Ernesto I'm curious of your tradeoffs of choosing doing this over just using a std::string?
Ernesto wrote: Hi:
Thanks a lot for your tips!
I am developing a library and I was thinking about implementing all my classes using my Pointer class template, by example, using:
void Array::AddElement(Pointer<T> aElement); Pointer<Array> Windows::GetComponents();
instead of
void Array::AddElement(T* aElement); Array* Widget::GetInnerWidgets();
Do you think it is a good idea? Or should I use my reference count Pointer internally (à la "copy on write" mechanism as in the string classes) and provide classical pointers in my API ? If it is a good idea, how should I handle the circular references?
Circular references need to be avoided by design. The correct use of
"weak" pointers is needed to ensure the correctness of the design.
When using "Austria C++" (shameless plug), I find that for reference
counted objects, there are almost no cases where non-smart pointers are
used. This is important for making sure that the code is also exception
safe. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: christopher diggins |
last post by:
I would like to survey how widespread the usage of smart pointers in C++
code is today. Any anecdotal experience about the frequency of usage of
smart pointer for dynamic allocation in your own...
|
by: He Shiming |
last post by:
Hi,
I'm having a little bit of trouble regarding pointer casting in my program.
I don't understand why the following two cases produce different results.
Case 1:
IInterface *pInterface = new...
|
by: Susan Baker |
last post by:
Hi,
I'm just reading about smart pointers..
I have some existing C code that I would like to provide wrapper classes
for. Specifically, I would like to provide wrappers for two stucts
defined...
|
by: maynard |
last post by:
How legit/acceptable is this bit of code?
class Base{
Base(){...}
Base(const Base& b){...}
};
class D1: public Base
{
....
|
by: Axter |
last post by:
I normally use a program call Doxygen to document my source
code.(http://www.stack.nl/~dimitri/doxygen)
This method works great for small and medium size projects, and you can
get good...
|
by: Jim Langston |
last post by:
Someone made the statement in a newsgroup that most C++ programmers use
smart pointers. His actual phrase was "most of us" but I really don't think
that most C++ programmers use smart pointers,...
|
by: Ney André de Mello Zunino |
last post by:
Hello.
I have written a simple reference-counting smart pointer class template
called RefCountPtr<T>. It works in conjunction with another class,
ReferenceCountable, which is responsible for the...
|
by: Boris |
last post by:
I had a 3 hours meeting today with some fellow programmers that are partly
not convinced about using smart pointers in C++. Their main concern is a
possible performance impact. I've been explaining...
|
by: johanatan |
last post by:
Does anyone know the reasons for the lack of an implicit casting
operator in any greater depth than:
A. Automatic conversion is believed to be too error prone.
(from the FAQ at the bottom of:...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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,...
| |