|
Hi, everyone!
When using vector.push_back method, we just copy an object
an not a reference. I think sometimes it is a waste. Am I
correct? I want to know how to let the vector to store
a reference.
Such is my sample code which can verify vector store copied
object and not reference.
Source Code:
--------
#include <vector>
#include <iostream>
using namespace std;
class A {
public:
int a;
};
int main()
{
A a;
a.a = 10;
vector<A> vc;
vc.push_back (a);
A b;
vector<A>::const_iterator i = vc.begin();
for (; i < vc.end(); i++)
{
b = *i;
b.a = 100;
}
cout << a.a << endl; //output is 10
return 1;
}
--------
So, how to let vector to store reference?
Thanks in advance,
George
|
|
|
|
|
George2 wrote:
So, how to let vector to store reference?
You can just create a vector containing pointers to object A:
A a, b, c;
vector<A*> vect;
vect.push_back(&a);
vect.push_back(&b);
vect.push_back(&c);
-Nick Parker
|
|
|
|
|
Thanks Nick buddy!
Are there some way to store reference directly?
You know, pointers often make troubles.
Thanks in advance,
George
|
|
|
|
|
George2 wrote:
Are there some way to store reference directly?
be careful here George. References can also be translated as an "alias", and may at times (with certain optimization) take up no space at all. What I am saying is there may be times when you use references the wrong way and things work in the debug mode (because the debugger assignes a 0 or NULL to a pointer), but in release mode you start to see troubles. Anyway, I don't want to get into too much detail, just make sure you use them properly.
- Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
|
|
|
|
|
Thanks, Nitron buddy!
Can you give me some materials to reference?
regards,
George
|
|
|
|
|
You can have a look at my article on pointers[^] for a start. In this article, I try to explain the concept of pointers and references from a physical memory-level standpoint while attempting to cut through all the non-pertinant information.
- Nitron
"Those that say a task is impossible shouldn't interrupt the ones who are doing it." - Chinese Proverb
|
|
|
|
|
I have a problem with Macros in Visual C++.
I want to write a more Detailed ErrorMessage Class, which gives me a part of sourcecode where the Error occurs.
There are some predefined Macros which will help me to that:
Current File: __FILE__
and Current Line: __LINE__
but I did not find a Macro for Current Call. So I want to make it my self.
#define __CALL__ "void myFunction(int value);"
void myFunction(int value)
{
printf("current Call is %s", __CALL__);
}
but problem is that I have to redo this procedure on every Function with additional undef:
#ifdef __CALL__
#undef __CALL__
#endif
#define __CALL__ "void myNextFunction(int value);"
So, I want to reduce manual work by autmating the following code:
#ifdef __CALL__
#undef __CALL__
#endif
#define __CALL__ "void myFunction(int value);"
so i want to do something like this (multiple Preprocessorcalls in one Macro:
#define SETFUNCTION(x) #ifdef __CALL__ #undef __CALL__ #endif #define __CALL__ x
But it all seems not to work or it is a hard handwork to insert this long code before each function.
So here are my Questions:
1. Is there any possibility to get easier more detailed Debug Information? (like current Function call)
2. Is there any way to make a Macro of Macros ? (like #define def(x) #ifndef x #define x #endif)
Thank you ,
Sendel
|
|
|
|
|
Putting the macro before the function definition will not work anyway, because the preprocessor will pass through the file first and keep #defining and #undefining SETFUNCTION, and finally SETFUNTION will only be the last function it processed. I thought of a macro like this:
#define SETFUNCTION(x) TCHAR __CALL__[] = _T(#x)
Put this as the first statement in all your functions, like this for example:
void myFunction(int value)
{
SETFUNCTION(myFunction);
...
}
and now __CALL__ will simply be a string locally declared within each function. Hope that helps.
|
|
|
|
|
Actually, putting the macro before the function may work, I'm not sure. Either way, putting a statement at the start of each function seems like a lot of work to me, but I can't think of a better way.
|
|
|
|
|
I want to display a text file into a view derived from CScrollView
how to display the file?
thanks!
|
|
|
|
|
|
How can I skip from one workspace to another?
For example, <ctrl+tab> for skipping between files, <alt+tab> for skipping between applications (win 98).
|
|
|
|
|
hello my name is Mario Young i'm from Colombia and i like C++
|
|
|
|
|
Welcome!
"Do unto others as you would have them do unto you." - Jesus
"An eye for an eye only makes the whole world blind." - Mahatma Gandhi
|
|
|
|
|
Mario_Young wrote:
hello my name is Mario Young i'm from Colombia and i like C++
Hi Mario,
For non-programming questions you might wanna check out the Lounge :-
The Lounge
Welcome to CP anyway
Nish
"I'm a bit bored at the moment so I'm thinking about writing a new programming language" - Colin Davies
My book :- Summer Love and Some more Cricket [New Win]
Review by Shog9 Click here for review[NW]
|
|
|
|
|
Strange name: "Mario Young i'm from Colombia and i like C++"
Welcome
|
|
|
|
|
Dear Freinds,
I have to do a customization of a product.There are dll in that project.I wish to have an Exit option using Escape key.I tried it using WM_KEYDOWN message but it is working with other keys like "F!,HOME,END etc except ESPCAPE key.Why?
Please let me know the solution for it.
Thanking you in advance.
With Regards,
Adarsh
|
|
|
|
|
You may intercept it using the PreTranslateMessage function.
Regards,
BB
|
|
|
|
|
Dear Bartosz
Will you pleasee explain it in detail.I am new also to VC and i have to cutomize a product which has dlls.
I have tried WM_KEYDOWN:
if (wParam)
case:VK_F1:
ExitAppliaction();
return(0);
case: VK_ESCAPE:
ExitApplication();
return(0);
It is working for F1 key but it is not working with Escape key.
So kindly give me the explanation.
Thanking you in advance..
With Regards,
Adarsh
I
|
|
|
|
|
Try this (SDI assumed):
BOOL CMainFreame::PreTranslateMessage(MSG* pMsg)<br />
{<br />
if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)<br />
{<br />
PostMessage(WM_QUIT);<br />
return TRUE;<br />
}<br />
<br />
return CFrameWnd::PreTranslateMessage(pMsg);<br />
}
Regards,
BB
|
|
|
|
|
Hey,
I've made a toolbar for IE but I have one problem. I don't know how to add tooltips to my buttons. Could anyone point me in the right direction??
Thanks.
|
|
|
|
|
Create the toolbar with the TBSTYLE_TOOLTIPS style, then handle the TTN_GETDISPINFO notification to provide the text for the tooltip.
--Mike--
Yeah, payin' the bills with my mad programming skillz.
Defraggin' my hard drive for thrills.
Homepage | RightClick-Encrypt | 1ClickPicGrabber
"You have Erica on the brain" - Jon Sagara to me
|
|
|
|
|
here is a bit code I just threw together.. ofcouse you would want to check the id of each controll and return a different string (either in the 80bytes allocated, or a pointer to one)
<br />
case WM_NOTIFY:<br />
{<br />
LPNMHDR pnmhdr = (LPNMHDR)lParam;<br />
switch (pnmhdr->code)<br />
{<br />
case TTN_GETDISPINFO:<br />
{<br />
LPNMTTDISPINFO lpnmtdi = (LPNMTTDISPINFO) lParam;<br />
if (lpnmtdi->hdr.idFrom == IDC_XXXX)<br />
{<br />
strcpy(lpnmtdi->szText, "hello");<br />
lpnmtdi->uFlags = TTF_DI_SETITEM;<br />
}<br />
}<br />
break;<br />
}<br />
}<br />
break;<br />
hope it helps
|
|
|
|
|
Michael Dunn wrote:
Yeah, payin' the bills with my mad programming skillz.
Defraggin' my hard drive for thrills.
Calm down Mike.
-Nick Parker
|
|
|
|
|
I'm writting one for fun and i've only just began to realize that it's quite a tricky process. The implementation i have used is probably not the best(nor is it finished) so i would like to here how maybe more experienced programmers have tackled similar tasks.
(is my lingo correct???)
I basically parse the statement into the following token categories:
1) Command token (SELECT, CREATE, etc)
2) Fields token (mytable.name, etc)
3) Tables token
4) Expression token (mytable.name = 'test')
5) Misc token(s) (ORDER BY, LIMIT, etc)
Once the statement is parsed into the following categories some tokens are further parsed by another parsing engine, like expression tokens will later be passed to an Expression evaluator where again it will be broken down into smaller tokens like < = > + * /, etc...
Anyways, from whats been said, does it sound like i'm on the right track???
Oh yeah, heres another important part:
I start parsing by extracting the COMMAND from the original string
So: SELECT * FROM temp WHERE name = 'Test'
Becomes: * FROM temp WHERE name = 'Test'
This is accomplished using the function CParse::ExtractCommand()
Then I would call CParse::ExtractFields() and so on...
Is there another way to accomplish this???
Any ideas???
Thanks
"Two wrongs don't make a right, but three lefts do!" - Alex Barylski
|
|
|
|