|
i don't understand you're problem...
you want to overload the operator=() for your class Test ?
nop, it's not actually what you want. here, you need an operator=() that returns an int . right ?
but what you can't do is reaching the (2,3) cell in your array.
so, you have to overload the operator()() ...
<font color=blue>class</font> Test {
<font color=blue>int</font> m_data[10][10];
<font color=blue>public</font>:
Test();
<font color=blue>virtual </font>~Test();
<font color=blue>void operator</font>=(<font color=blue>int</font>); <font color=green>
<font color=blue>int</font>& <font color=blue>operator</font>()(<font color=blue>int</font> x, <font color=blue>int</font> y);
};
<font color=blue>int</font>& Test::<font color=blue>operator</font>()(<font color=blue>int</font> x, <font color=blue>int</font> y) {
<font color=green>
<font color=blue>if</font> (x < 0) x = 0;
<font color=blue>if</font> (y < 0) y = 0;
<font color=blue>if</font> (x > 10) x = 10;
<font color=blue>if</font> (y < 10) y = 10;
<font color=green>
<font color=blue>int</font>& ri = m_data[x][y];
<font color=blue>return</font> ri;
}
this way, when you do:
Test tst;
tst(2, 3) <font color=green>
= 20; <font color=green>
ASSERT(tst(2,3) == 20);
Test *pTst = &tst; <font color=green>
(*pTst)(2, 3) = 21; <font color=green>
ASSERT(tst(2,3) == 21);
cheers,
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
It is not the = operator that you need to overload but rather the [] operator. But in your case you have two nestled [] operators so you will need to make use of an intermediate class (like RowTest).
Something like that:
<br />
class RowTest<br />
{<br />
int m_data[10];<br />
public:<br />
int& operator[](int x);<br />
};<br />
<br />
<br />
class Test<br />
{<br />
RowTest m_data[10];<br />
public:<br />
RowTest& operator[](int x);<br />
};<br />
Then, by overloading this operator you will be able to do this:
Test tst;<br />
tst[2][3] = 20;
But of course there is an extra class involved. The syntax is perhaps not correct because I don't play a lot with these kind of things.
Hope this helps
|
|
|
|
|
nop, not the operator[]() because it can get only once value between the [ ] ...
prefer operator ()() ...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Yes, that's why you need to make use of an intermediate class... But ok, it may be a little bit too complicate. The other solutions were simplier
|
|
|
|
|
I dont need extra class I'm going to check out of range x,y for m_data[x][y] in overloaded operator so I need that class will act as an array.
With simple array we can assign a value to it and read it:
int val;
val = m_data[x][y];
m_data[x][y] = val;
All this I want to be done by class
class Test
{
int m_data[10][10];
public:
int operator()(int x, int y);
};
int Test::operator()(int x, int y)
{
//check if x,y out of range
return m_data[y][x];
}
1. so we can read value by overloaded () operator
Test tst;
int val = tst(2,3); //which equ to val = m_data[x][y]
2. how to implement m_data[x][y] = val by Test class and what operator to overload?
if we overload just = operator then we will be able just
Test tst;
tst = 5; //but its not possible with this to provide x,y coeffs for m_data[x][y]
9ine
|
|
|
|
|
9ine wrote:
int Test::operator()(int x, int y)
{
//check if x,y out of range
return m_data[y][x];
}
it won't work until it returns an int (copy of the actual value... so, it is not an lvalue, an the compiler won't accept you to assign the result of the operator()() .
use int& instead, as i did in my first answer[^]...
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
And how to be with class pointers?
with Test tst;
we get nice brief form:
int a = tst(3,2);
but with class pointer Test *tst1 = &tst;
we get bulky notation:
int b = tst1->operator()(3,2);
is it possible to something like tst(3,2) with pointer to class?
9ine
|
|
|
|
|
wohhhhh, no no no
use the defenrenciation operator...
Test *tst1 = &tst;
int b = (*tst1)(3, 2);
hope this helps you
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Thanks very much, I've done it completely and it works!
9ine
|
|
|
|
|
9ine wrote:
Thanks very much, I've done it completely and it works!
you're welcome... i'm always happy when i reach helping people usefully...
ps: if you liked my post, don't forget to vote for it !
ps 2 : i edited my previous answer[^] to consider using pointers.
cheers dear,
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Hi,
I'm using VC7 and MFC.
I create a ListBox control using the dialog editor and the toolbox and I attach it a control variable of the type CListbox.
On the other hand I create another list manually, declaring a CListBox variable and calling Create.
I use AddString to add new items, but the fonts are different. The manually created list has some kind of bold font.
I have tried everything. Changing the create styles, trying to use CreateEx, but I cannot get they to look the same.
I have the same problem with combo boxes.
I would appreciate any idea. Can anybody help me?
|
|
|
|
|
I think that the code that is generated for you to create the control you dragged on includes code to set the font. I believe it has a Font property, or something like that.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Thank you very much for your fast response.
I decided to create those controls manually because I have a dialog window with edit controls, list boxes and combo boxes.
If I use the dialog editor and attach the control variable to them, some work properly when I add data, but others raise the error ERROR_INVALID_WINDOW_HANDLE.
And I do not destroy the dialog window or the controls.
Thank you again for our fast response.
|
|
|
|
|
mpuerto wrote:
If I use the dialog editor and attach the control variable to them
How do you mean ?
Another way to do this if you have multiple things on the form, is to put them all there, and just hide the ones you don't want to see at any point in time.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I meant that I place a list box in the dialog, with the dialog editor (not coding, with the visual interface), right from the toolbox. Then I attach a control variable to the listbox with the wizard.
When I try to add new strings to the listbox with AddString, it raises the error I told you before. But if I do everything coding: declare a variable of type CListBox, call Create and then AddString it works properly.
Can it has something to do with the fact that I declare the dialog class as DECLARE_DYNAMIC?
Thank you again for the help and sorry for the misunderstanding.
|
|
|
|
|
Oh, I get you now. I'm sorry, I really don't know the answer to that question.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
mpuerto wrote:
When I try to add new strings to the listbox with AddString, it raises the error I told you before.
Any chance you are calling AddString() before the execution of your dialog's OnInitDialog()?
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
Sorry, I think that's not the case. Because I call AddString when I press a button in the dialog. If I call InitStorage the same error takes place.
Thank you for answering.
|
|
|
|
|
When you create a control by adding it via the dialog editor, the control automagically uses the font of the dialog. If you create the control manually via Create() you have to set the font yourself, or else it will use the default system font. You can use GetFont to get the dialog's font, and SetFont to set the control's font.
CMyListBox.Create(...);
CMyListBox.SetFont(GetFont());
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03
"Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04
"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05
Within you lies the power for good - Use it!
|
|
|
|
|
I am working on a VC++ project in which
UserInterface is an
exe and also a service is running for the
application.My problem is
when i switch between one user in WIN XP to another I
am not able to pop
up the userinterface in new user environment.My
service is autmatic on
task manager i am able to get userinterface exe for a
while but it dies
.when we check the task manager of first user I can
see both
usrinterface and the related service on task
manger.How can i pop up my user
interface when we switch from one user to another.
|
|
|
|
|
Hello Friends,
Kindly observe the code given below
#include <iostream.h>
void show(int x)
{
cout <<"inside the function one " << x << endl;
}
template <class t=""> void show( T a)
{
cout << "inside the template" << a << endl;
}
int main()
{
show(10);
return 0;
}
When I run this program , I get the output
"inside the function one 10".
My question is why the template function is not get executed
and only simple function get executed only"void show(int x)".
Can any one help me in this matter?
|
|
|
|
|
You need to check 'Do not treat <'s as HTML tags' for us to see the code properly.
Christian Graus - Microsoft MVP - C++
#include <iostream.h>
void show(int x)
{
cout <<"inside the function one " << x << endl;
}
template <class T> void show( T a)
{
cout << "inside the template" << a << endl;
}
int main()
{
show(10);
return 0;
}
When I run this program , I get the output
"inside the function one 10".
|
|
|
|
|
phijophlip wrote:
int main()
{
show(10);
return 0;
}
Of course you do, what else could happen ?
show(10) will call the non-templated function, and without that function, would be a compiler error
show<int>(10) will call the second function and will not compile if only the non templated function exists.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
i am working on making a mediaplayer.I have to add directories at run time.The files in the folder path should be added to list control along with existing files .iam using SHBrowseForFolder .I am adding foler path to environment variable.List should update and display all files according to the path in environment variable set .List should reside on reboot . Now it is showing the most recently navigated folder.On reboot it 's null.
all files are video files .
Thanks for going through my message
|
|
|
|
|
geo19762005 wrote:
List should reside on reboot
trying to understand the above line, can you please help.
suhredayan There is no spoon.
|
|
|
|