|
Dear all,
I cannot quite understand the logic behind the notion of operator overloading.
I understand that creating a class, is like creating a new type, therefore we can overload some operators to work with that new type. For example:
Ratio& Ratio::operator=(const Ratio& r)
{
num = r.num;
den = r.den;
return *this;
} This example is absolutely comprehensible but I cannot understand how I have to think, visualize the things in order to implement operator overloading in the *, *= operator, or in the == for instance.
Consider the Ratio class as the following:
class Ratio
{
friend Ratio& operator*(const Ratio& x, const Ratio y);
public:
Ratio(int =0, int =1);
Ratio& operator=(const Ratio&);
Ratio& operator*=(const Ratio&);
private:
int num, den;
};
Here is the understanding problem:
Ratio operator*(const Ratio& x, const Ratio& y)
{
Ratio z(x.num*y.num, x.den*y.den);
return z;
}
Is this the only way to overload that operator in this example? How one has to think in order to writew the above code? What are the steps involved in order to write the above two lines of code? And secondly in the first example (operator=) the return type is a reference to the Ratio but in the second is not a reference. Why is not a reference. When should we use a reference and when not?
Please bare in mind that I understand the use of friend functions.
Thanks in advance for the help,
grscot
|
|
|
|
|
you have to consider what it means to multiply the two things together, and then impliment this in code. It's as simple as that.
BTW, you're posting in the managed C++ forum - why ?
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
I am new to Managed C++, How can i conver the System.String to wchar_t (Unsigned 16bit).
Regards,
Murali.
|
|
|
|
|
use object oriented char converter class (my own one). Email me I will send it to you.
Cheers
MK
cheers
MK
|
|
|
|
|
I want convert managed C++ System.String type to C style, char, tchar, _bstr types. How can i conver to un managed c++ types.
Please help..
Murali.
|
|
|
|
|
If you don't mind using CString, it can take a System::String in its ctor.
|
|
|
|
|
Hai
u can convert it using the InteropServices
const char* str=(const char*)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi("ManagedString")).ToPointer();
By
Anish
|
|
|
|
|
can someone help me with iterating through a map. i am using a tree layout and only using the outer edges. im at the very bottom of the map and i want to get back to the root.
for example a char tree:
c
/ \
b d
/ \
a e
lets say i start at e, and the key is e and value is d, how do i get back to c and stop there, the key and value are equal at c, and print everything out. thanks a bunch
|
|
|
|
|
How do you turn a map into a tree ? Is it a map of maps ?
In any case, you're in the managed C++ forum.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
its just a map, where the value points to its parent node, like a tree.
|
|
|
|
|
So it's not a std::map then ? Or am I missing something obvious ?
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
clarify please;)
cheers
MK
|
|
|
|
|
wow, i guess im confusing people. let me try again. i have a map with 'char' in them. and each map takes two elements, a key and a value. so i inserted in the map the 'char' as the key, and its predecessor as the value, for example:
key, value
e, d
d, c
c, b
b, a
a, a
i want to start from the 'char' e, and iterate through the map until i reach a, where the key and value are the same. how do i go about doing this, i am new to c++ and the template libray. thanks
|
|
|
|
|
i have written complete code for this program and it is running almost fine it is just not giving proper display in the last screen in the output.
please help me just reply me so that i will send you the source code files and if you want i can connect through netmeeting to show what is going wrong on the output screen. I am also available on msn as youngashish@hotmail.com
and at yahoo as young_ashish@yahoo.com
please please help me
just reply me so that i can send you the source code files.
|
|
|
|
|
No-one is going to help you, because you're going about it all wrong.
First of all, is your program managed C++ ? If not, you posted in a place that few people go, so no-one will see it.
Secondly, if you have a problem, you should post the code here and ask about it. You should certainly take hte time to know enough about what is going on to know where in your code it fails. Have you stepped through your code in a debugger ?
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
i will post the code can you help me i just wanna have the small bug being resolved.
please do reply me as soon as possible.
i will send you the code also
|
|
|
|
|
youngashish wrote:
i will send you the code also
Email me the code, and you will have to die.
Getting back to my original point though - is it in managed C++ ?
If you post it in the right forum, I'll be glad to help, and so will others, I am sure.
Christian
I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
|
|
|
|
|
I can figure out how to do this with normal classes, but with managed forms, I am totally stumped. All I wanna do is communicate between forms. In this example, I wanna make the current form invisible, and make the other form visible, but after 2 weeks, still can't do it. I've had responses from people on it, but they only work for unmanaged classes, not these strange manged type things.
The problem is trying to get the second form to point to the first one when it hasn't yet been fully defined. Get errors such as
error C2512: 'datapassing::Form1' : no appropriate default constructor available
Please someone help me,
Using Microsoft Visual C++.NET V2003
I could manage forms untill .NET decided to manage them for me!!
**Form1.h**
<br />
#pragma once<br />
#include "Form2.h"<br />
<br />
namespace datapassing<br />
{<br />
<br />
public __gc class Form1 : public System::Windows::Forms::Form<br />
{ <br />
public:<br />
Form2 *pFrm2;<br />
public:<br />
Form1(void)<br />
{<br />
InitializeComponent();<br />
pFrm2 = new Form2;<br />
}<br />
<br />
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)<br />
{<br />
pFrm2->Visible = true;<br />
this->Visible = false;<br />
}<br />
};<br />
}<br />
**Form2.h**
<br />
#pragma once<br />
<br />
namespace datapassing<br />
{<br />
public __gc class Form1;<br />
public __gc class Form2 : public System::Windows::Forms::Form<br />
{<br />
public: <br />
Form1 *pFrm1;<br />
public: <br />
Form2(void)<br />
{<br />
InitializeComponent();<br />
pFrm1 = new Form1;<br />
}<br />
<br />
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)<br />
{<br />
pFrm1->Visible = true;<br />
this->Visible = false;<br />
<br />
}<br />
};<br />
}<br />
|
|
|
|
|
Hai
U can done it very simply. Don't include or declare Form1 in Form2 . It will produce circular referencing.
change ur code like this
**Form1.h**
#pragma once
#include "Form2.h"
namespace datapassing
{
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form2 *pFrm2;
public:
Form1(void)
{
InitializeComponent();
pFrm2 = new Form2;
pFrm2->Owner=this;
}
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
pFrm2->Visible = true;
this->Visible = false;
}
};
}
**Form2.h**
#pragma once
namespace datapassing
{
public __gc class Form2 : public System::Windows::Forms::Form
{
public:
public:
Form2(void)
{
InitializeComponent();
}
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{
this->Owner->Visible = true;
this->Visible = false;
}
};
}
By
Anish
|
|
|
|
|
This is fantastic, thanks, works just fine. One further thing though.
I have a text box textBox1 in form 1. I need to update it from form 2, I assumed that if I could make the form visible/invisible, I could also update the text box, so using your code I tried
this->Owner->textBox1->Text = "Updated Text";
This doesn't work, as the Owner only seems to be able to see the general form attributes, not the actual components of the form. How therefore do I change things such as text in the text box in form1, dependant upon an action in form2. If anyone can solve this problem for me (Been trying to figure it for 2 weeks now, but no-one is telling), I will consider you a god, and worship you daily.
Thanks
Ultimate Newbie
|
|
|
|
|
Hai,
i dont know the what i am saying is correct. but ur probelm can be solved in this way. But this is not recommended if u have heavy updation in ur Form1 class. One more reason for my qucik response is that next four days will not here.
simply pass a reference of ur textbox to the Form2 class
ie
Create a TExtbox object in ur Form2 class;
TextBox* t1;
Passes the reference of original one to this value in Constructor
frm2->t1=this->textBox1;
Now u can update the text in Button click as
this->tt->Text="Updated Text";
By
Anish
If u find a good solution plz post it here
|
|
|
|
|
I'm kind of new to CLI but does anyone know why this won't compile?
#include "stdafx.h"
using namespace System;
using namespace System::Data;
using namespace System::Data::SqlClient;
int _tmain()
{
SqlConnection ^myConn = gcnew SqlConnection("valid connection string");
myConn->Open();
if(myConn->State == ConnectionState::Open)
myConn->Close();
}
Its the default .NET console wizard in 2005 Express. I get errors a lot like this: error C3624: 'System::EnterpriseServices::ITransaction': the compiler cannot find this type;
it is defined in the assembly 'System.EnterpriseServices' (with various namespaces). Doesn't make any sense to me.
Matt Newman
...armed with what? spitballs!? - Zell Miller
|
|
|
|
|
|
Didn't know you had to add references like that, I guess C# does that automagically.
Matt Newman
...armed with what? spitballs!? - Zell Miller
|
|
|
|
|
I am using Visual Studio.Net 2003
My mainThread creates a newThread and the newThread starts doing its thing. At some point the newThread suspends itself; at which point it enters the ThreadState of Suspended.
I am trying to abort the newTread from my mainThread. When i attempt this using a newThread->abort();
It throws a ThreadStateException, i beleive because the newThread is suspended.
At this point the ThreadState of newThread is (SuspendRequested, AbortRequested).
So in the ThreadStateException catch, i use a newThread->Resume();
This causes the threadState of newThread to be simply (AbortRequested).
The problem is that I can't figure out how to get the newThread to leave AbortRequested state and enter the Stopped state.
If I call the abort from within newThread, it enters the Stopped state without problem; why can't I abort the newThread from within mainThread?
any help would be appreciated,
Thanks,
Jody Blau
|
|
|
|