|
Hello everyone:
What is the difference the following two declarations, what are their advantage and
disadvantage? Or difference usage? Can anyone show me an example?
----------
typedef enum foo_ {
CON_1,
CON_2
}foo;
----------
and
----------
enum goo_ {
CON_1,
CON_2
}
----------
Thanks in advance,
Geo
|
|
|
|
|
http://www.codeguru.com/forum/showthread.php?s=&threadid=194304
|
|
|
|
|
Thanks, Alex buddy!
It is really a nice thread.
regards,
George
|
|
|
|
|
In C, enum s, struct s and union s have their own namespaces. To use them elsewhere, you must say (for example) enum goo_ .
The use of typedef adds a new name to the global type namespace, where the name can be used without qualifiers. Therefore you can declare a variable of type enum foo_ just by using the type name foo .
C++ removes this distinction; while enum s may still be referenced by prefixing enum , this is no longer required. This may break some C code if the C code uses the same name for both an enum and a typedef . Therefore all of foo , foo_ and goo_ can be referenced directly. You can still refer to foo_ and goo_ as enum foo_ and enum goo_ respectively.
Examples:
foo bar; <font color="green"></font>
foo_ bar2; <font color="green"></font>
enum foo_ bar3; <font color="green"></font>
goo_ bar4; <font color="green"></font>
enum goo_ bar5; <font color="green"></font>
struct foo { int i; }; <font color="green"></font>
int foo_; <font color="green"></font>
|
|
|
|
|
Thanks, Mike buddy!
I do not understand the why the following two statements are OK or error, can you give me a simple explanation?
Quote:
----------
struct foo { int i; }; /* C: OK, C++: error */
int foo_; /* C: OK, C++: error (IIRC) */
----------
regards,
Geo
|
|
|
|
|
OK, let me try to explain namespaces again, a bit clearer this time hopefully!
Let's start with the global scope. In this scope, you can declare variables, functions and (through the typedef keyword) types.
In C, names that are introduced with the struct , union or enum keywords share another, separate, namespace which I'll call the structure namespace. Names can exist in both the global and the structure namespace with no problems. To access a name in the structure namespace, you must use the same keyword you used to declare it (i.e. if foo is an enumeration, you must use enum foo ).
typedef can always be used to add a type name to the global namespace, whatever namespace the original type came from.
C++ changes the rules a bit. Types declared with the struct , union , enum and class keywords are still in a structure namespace; however, you no longer need to introduce them with the keyword when they're used.
Let's reproduce the example so it compiles:
1 typedef enum foo_
2 {
3 FOO_BAR_1,
4 FOO_BAR_2
5 } foo;
6
7 enum goo_
8 {
9 GOO_BAR_1,
10 GOO_BAR_2
11 };
12
13 struct foo { int i; };
14
15 int foo_; The declaration at line 13 is NOT an error in C because the earlier foo at line 5 is a typedef name and hence is in the global namespace, while the declaration at 13 creates a foo in the structure namespace. It IS an error in C++ because it tries to introduce a new type with the same name.
However, I was wrong about line 15. It's OK in both C and C++. While you can't have a variable with the same name as a type in the global namespace, you're permitted to have one with the same name as a type in the structure namespace.
Results were compared with VC6 and with Comeau Computing's online compiler[^].
Note that I had to change the names of your enumerators (the values of the foo_ and goo_ enumerations). These are also added to the global namespace, and therefore would clash with each other if they had the same names.
Now to scopes: there is a 'global' and a 'structure' namespace within each scope that names can be declared in. A namespace using the C++ keyword is a named scope. Therefore you can refer to, for example, 'std::struct pair '.
|
|
|
|
|
Thanks, Mike buddy!
George
|
|
|
|
|
Hi there
I create a vector of size(10)
using a for loop to insert values into the vector
for(int i = 0; i < 10; ++i)<br />
v.push_back(i + 0.1);<br />
<br />
for(int i = 0; i < 10; ++i)<br />
cout << v[i] << "\n";
Only display zero's on screen, When i dont put in an initial size the vector diplays what i want.
is it possible and to use v.size() rather than 10
in the for loops, but also displays zero's and warings
at compile time.
Am i doing something wrong!
Help!
Regards bhangie
|
|
|
|
|
is the vector a type of std::vector or is it CArray???
look at this example for std::vector -
int main( )
{
using namespace std;
vector <int> v1;
v1.push_back( 10 );
v1.push_back( 20 );
const int &i = v1.at( 0 );
int &j = v1.at( 1 );
cout << "The first element is " << i << endl;
cout << "The second element is " << j << endl;
}
notice that the item is retrieved with at() function and not with the operator [] .......
do the same for CArray using GetAt() method instead of at()
hope this helps
Yaron
Ask not what your application can do for you,
Ask what you can do for your application
|
|
|
|
|
With
std::vector< double > v(10);
you create a vector of 10 elements with the value of 0.0
Later, with
v.push_back()
you add elements at the end of the vector.
When you display the first 10 items of the vector, you only show the zeros inserted at construction.
When you let your loop run to v.size() , (make i an unsigned int , or better still, std::vector< double >::size_type ) it should display all you did put into the vector.
Hope this did clear things up for you
Who is 'General Failure'? And why is he reading my harddisk?!?
|
|
|
|
|
Thanks YaronNir
std::vector
cleared up a few stuff
? 0.1,......up until 999.1
push_back(?)(or i + 0.1)
display all contents in vector
bhangie
regards
|
|
|
|
|
Thanks oaks
got a better better understanding now!
bhangie
|
|
|
|
|
Just as an excercise -
Here is how to use iterators for the display:
#include "stdafx.h"
#include "vector"
#include "iostream"
#include "string"
int _tmain(int argc, _TCHAR* argv[])
{
std::vector< double > v(10);
for(int i = 0; i < 10; ++i)
v.push_back(i + 0.1);
std::vector< double >::iterator it = v.begin();
while (it != v.end())
{
std::cout << *it << "\n";
++it;
}
std::string s;
std::cin >> s;
}
Who is 'General Failure'? And why is he reading my harddisk?!?
|
|
|
|
|
i have a dilaog, on it i've placed a picture (from the resource controls)
i set the properties to icon.
the icon i have in the resources is 16x16, but when the dilaog is shown i get an icon image of 32x32.....why is that?
can any1 help me here? i need to display the 16x16 image (and i do not want to use a bitmap)
thanks in advanced
Yaron
Ask not what your application can do for you,
Ask what you can do for your application
|
|
|
|
|
In the properties of the picture frame.. Go to styles and check the real size image.. It will still look large inside the development environment but once you execute the program you will see that it does in fact show up as 16x16.
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
thanks man
that really helped me!
Yaron
Ask not what your application can do for you,
Ask what you can do for your application
|
|
|
|
|
no problem..
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
CPaintDC dc(this);<br />
<br />
CRect r(0,0,10,10);<br />
CBrush brush(RGB(0,0,0));<br />
<br />
dc.FillRect(&r,&brush);<br />
This piece of code draws a filled rectangle at position 0,0.
How can I change the offset of the DC, that the rectangle will paint at, for instance, (x50,y50), without changing the CRect object itself?
I've tried some of the OffsetViewportOrg and SetWindowOrg methods, but I can't seem to make it work. It only changes the clip region.
I've tried SetBrushOrg too, together with Unrealize and SelectObject, but that doesn't change anything at all. If I read the msdn right, this is the function to use! But how?
How can I change the DC's offset?
using: [VISUAL STUDIO 6.0 sp5] [WIN98/2]
|
|
|
|
|
|
dwccgc wrote:
Did you try SetViewportOrg()?
Yes, it only moves the clip rectangle. The rectangle itself is still painted at 0,0.
dwccgc wrote:
Did you check your "Mapping Modes"?
Mapmode is 1 (==MM_TEXT).
I think I have to use the SetBrushOrg, but that doesn't seem to work.
using: [VISUAL STUDIO 6.0 sp5] [WIN98/2]
|
|
|
|
|
I found some old code where I draw a Polygon. I use CClientDC instead of CPaintDC.
CMyView::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CClientDC dc(this);
..
..
CRect rcArea(lpDrawItemStruct->rcItem);
rcArea.DeflateRect(20, 20);
dc.SetViewportOrg(rcArea.left, rcArea.bottom);
..
..
dc.Polygon(PolyPoints.pt, PolyPoints.nCoord);
..
..
}
Did you have any luck with that MS Scribble Link I left on the previous message?
|
|
|
|
|
I'll try it.
No, I don't have the examples, and there's no download at that page.
using: [VISUAL STUDIO 6.0 sp5] [WIN98/2]
|
|
|
|
|
|
dwccgc wrote:
You can also find the samples on your MSDN Library Disks
I know, but I don't have the disks either...
But, meanwhile, I found a solution (workaround).
Instead of this:
CPaintDC dc(this)<br />
Use this:
PAINTSTRUCT ps;<br />
CDC* pDC=NULL;<br />
pDC=BeginPaint(&ps);<br />
<br />
...<br />
<br />
EndPaint(&ps);<br />
With the second code, the DC's offsets work the way they should. I don't know why they don't work with CPaintDC (nor CClientDC).
using: [VISUAL STUDIO 6.0 sp5] [WIN98/2]
|
|
|
|
|
Dear ALL,
I am developing an application for Windows 2000 in MFC VC++ Version 6.0.
My application runs as an interactive service.
I am using a dll in my program which is used to hide given key(s) from the registry. The program works fine and hides the given keys from registry. But when i log off my system and login, the following error occurs:
...............................
SAS Window: Winlogon.exe Application error.
The instruction at "0x01007f2d" refereced memory at "0x00006c". The memory could not be read. Click ok to close and cancel to debug.
...............................
On clicking ok the system restarts.
I think I need to free the dll before logging out. So I tried FreeLibrary in QueryEndSession, but it didnot helped.
Is there any other way to free the dll.
Or any other way out to solve the problem ?
The program is working fine when I restart and login (probably because at that time the dll is freed and loaded again,, but the same is not in case of log off and log in)
Thanks
Rohit
|
|
|
|