473,398 Members | 2,335 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,398 software developers and data experts.

Help adding objects

I would like to do something like the following

class TMyClass
{
......
}

TMyClass* mc1 = new TMyClass;
TMyClass* mc2 = new TMyClass;
TMyClass* mc3=mc1+mc2;

Where the addition mc1+mc2 would take the desired information from each
object, put them together , then store them mc3. I tried overloading the
operator+, but when I compile the mc3=mc1+mc2 line, the compiler complains
about invalid pointer addition.

Anyone have an ideas?

Thanks for any and all help. It is greatly appreciated.
Brian Dumas


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #1
3 1340
Brian Dumas wrote:
I would like to do something like the following

class TMyClass
{
.....
}

TMyClass* mc1 = new TMyClass;
TMyClass* mc2 = new TMyClass;
TMyClass* mc3=mc1+mc2;


mc1 and mc2 are pointers and it makes no sense to add two pointers ever.

You could try

TMyClass* mc3 = new TMyClass(*mc1+*mc2);

where you overload the + operator suitably and define a copy constructor
if needed.
Jul 22 '05 #2
Brian Dumas wrote:
I would like to do something like the following

class TMyClass
{
.....
}

TMyClass* mc1 = new TMyClass;
TMyClass* mc2 = new TMyClass;
TMyClass* mc3=mc1+mc2;

Where the addition mc1+mc2 would take the desired information from each
object, put them together , then store them mc3. I tried overloading the
operator+, but when I compile the mc3=mc1+mc2 line, the compiler complains
about invalid pointer addition.

Anyone have an ideas?

Thanks for any and all help. It is greatly appreciated.
Brian Dumas


In your example above, all the variables are pointers to
the classes, not instances of the classes. The compiler
is correct, because you are telling it to add pointers
together.

I think you meant:
TMyClass * mc3 = new TMyClass; // create an instance for the result.
*mc3 = *mc1 + *mc2; // Add the instances, not the pointers.

BTW, there is no requirement for classes to start with a 'T'
or a 'C'. Feel free to be different.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #3
On Wed, 11 Feb 2004 11:30:09 -0800 in comp.lang.c++, "Brian Dumas"
<br***@kvdco.com> was alleged to have written:
operator+, but when I compile the mc3=mc1+mc2 line, the compiler complains
about invalid pointer addition.


Right. Stop using pointers. Stop using "new".
Put down that Java book and step away from it.

Jul 22 '05 #4

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

Similar topics

1
by: Geoff Biggs | last post by:
Evening all, I'm trying to add a new built-in number data type to Python with its own syntax, so I'm working directly with the interpreter rather than creating my own extension module (side...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
6
by: JustSomeGuy | last post by:
I have an stl list that grows to be too huge to maintain effectivly in memory. There are elements within the list that could be stored on disk until accessed. However I don't want to expose...
2
by: jm | last post by:
I am trying to do something VERY simple. I want to use this: http://www.codeproject.com/csharp/dotnetbandobjects.asp?msg=727553#xx714692xx to point to a web page. I cannot use process.start...
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
4
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO...
6
by: lennon1 | last post by:
Hi, I have already started learning .NET and I have a question. If I want to do anything - Display Data, Navigate, Update - with database (SQL Server) in Visual Studio 2005, do I have to use all...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
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: 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...
0
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...
0
Oralloy
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,...
0
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...
0
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...
0
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...
0
agi2029
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,...
0
isladogs
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...

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.