|
Code:
class MyClass
{
public:
MyClass(int iType);
};
class YourClass
{
public:
YourClass();
MyClass mys[2];
};
How to init constructor of MyClass for array mys[2] at:
YourClass::YourClass()
{
}
Impossible?
modified on Sunday, June 20, 2010 7:44 PM
|
|
|
|
|
You can create an Init method and call it for each element in the array.
|
|
|
|
|
Good question. I can't believe I've never ran into this before. I don't see any clean way around it.
Steve
|
|
|
|
|
I've come across this a couple of times before and I didn't find any other method than to assign values separately to each object in the array.
|
|
|
|
|
Unfortunately, the language don't specify any construct to pass parameters to the array constructing elements.
std::vector does this, essentially with a trick (to allow array grow and shrink it doesn't allocate "array" but "bulk buffers".
You can emulate this trick yourself this way:
class Element
{
public:
Element(ParamType _a)
{ ... }
private:
};
static const size_t BUFSZ = ...;
class Aggregate
{
public:
Aggregate(ParamType _a) :vector()
{
vector = reinterpret_cast<Element*>(buffer);
for(size_t i=0; i<BUFSZ; ++i)
new(vector+i)Element(_a);
}
Aggregate(const Aggregate& _s)
{
vector = reinterpret_cast<Element*>(buffer);
for(size_t i=0; i<BUFSZ; ++i)
new(vector+i)Element(_s.vector[i]);
}
~Aggregate()
{
for(size_t i=0; i<BUFSZ; ++i)
vector[i].~Element();
}
private:
char buffer[BUFSZ*sizeof(Element)];
Element* vector;
};
The advantage respect to std::vector is that memory is not allocated dynamically, but - Being BUFZ statically note, it can stay into the aggregate space.
The "other way" is to let Element s to default-construct someway, and than assign them the required values.
It mostly depend on the fact you can -or not- accept a default initialization for Element-s putting them in a sort of invalid state until they are assigned.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
If you don't have to use an array (and generally there's no point), can use a vector and want each element initialised to the same value:
class YourClass
{
public:
YourClass() : mys( 2, MyClass( 7 ) )
{}
std::vector<MyClass> mys;
};
will do the trick. If you want different values then it's a bit trickier unless they're known at compile time. I won't bother going over that here though, please post if you want to know how to do it.
If you want them to all have different values and have an array set at compile time you're going to have to manage the construction and destruction yourself into a raw memory buffer:
class B
{
public:
B( int ) {}
};
class A
{
public:
A() : values_( reinterpret_cast<B (&)[2]>( raw_buff_[ 0 ] ) )
{
}
private:
char raw_buff_[ sizeof( B[2] ) ];
B (&values_)[2];
};
How you write the body of the constructor for A then depends on whether you want one value or different values. If you want the same value you can use initialized_fill:
A() : values_( reinterpret_cast<B (&)[2]>( raw_buff_[ 0 ] ) )
{
std::uninitialized_fill( &values_[ 0 ], &values_[ 2 ], B( 7 ) );
}
If you want to have different values you'll have to use something like construct (if your standard library supports it, it was in the SGI STL but not in the standard) or write your own:
template <class T1, class T2> void construct(T1* p, const T2& value)
{
new( p ) T1( value );
}
and then use it to implement your constructor:
A() : values_( reinterpret_cast<B (&)[2]>( raw_buff_[ 0 ] ) )
{
construct( &values_[ 0 ], B( 1 ) );
construct( &values_[ 1 ], B( 2 ) );
}
In either case you're going to want a destructor as you have to manually run the destructor for the elements in the array:
template <class T> void destroy(T* p)
{
p->~T();
}
~A()
{
destory( &values_[ 0 ] );
destroy( &values_[ 1 ] );
} Generally don't use C-style arrays unless you really have to. There's usually a solution in the standard library that's (almost) as good and far less fiddly to use.
Cheers,
Ash
Edited: Forgot the destructor.
|
|
|
|
|
Hi,
I am tracing code with the Visual Studio Debugger and when I hit upon CString::GetAt(8) statment
seems like I lose control meaning the statment Following the GetAt is a "if"
statement but when the debugger encounters the GetAt it takes off probably to the codde where the GetAt
ia and never returns
|
|
|
|
|
You question is not very clear to me.
I'm assuming you're having trouble when stepping into the MFC code.
You can use the following shortcut keys while debugging -
F10 will step over the function.
F11 will step into the function.
Shift + F11 will step out of the function.
Here is a link to the complete list of debugging shortcut keys -
Debugger Shortcut Keys[^]
|
|
|
|
|
when I step thru it
it just goes off somewhere
meaning the yellow arrow disappears and my main window is displayed
seems like it bypassed executing some code
|
|
|
|
|
i am receving data from serial port and i have to display in two dialog boxes,but the data is not being received in second dialog.The first dialog is parent of second dialog.I am using MSComm ActiveX controls. i am closing the port and opening again.
Please help me to solve the problem.
Thanks in advance
|
|
|
|
|
There is probably no data available to read.
The serial port by default acts like a pipe.
Data is removed from the port after it is read.
So the parent dialog is probably reading all the data leaving nothing available for the child dialog.
Also, please do all error checking and see if any of the calls are failing.
|
|
|
|
|
If I were you, I would write a wrapper around your serial port (and most probably get rid of MSComm active X). This class would then store the data so that mutliple parts of your application can retrieve the required information.
|
|
|
|
|
#include <iostream.h>
int main()
{
int x=1,y;
y= ++x*++x;
cout << x << endl;
cout << y <<endl;
return 0;
}
|
|
|
|
|
Because you shouldn't use the pre-increment operator that way.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hello
It's not very hard. answer of this simple question is :
YOU WRITE ++x TWICE.y=++x[first time]*++x[second time]
don't forget to vote me.
BYE...
|
|
|
|
|
++x
*
++x
You should not use expressions like this as they may not give the results you expect.
It's time for a new signature.
|
|
|
|
|
Richard MacCutchan wrote: You should not use expressions like this as they may not give the results you expect.
I welcome you to take a look at our codebase.
Workout progress:
Current arm size: 14.4in
Desired arm size: 18in
Next Target: 15.4in by Dec 2010
Current training method: HIT
|
|
|
|
|
Note that priority of ++ operation is higher than *.If you have y= ++x*x++ it should be 4.
I would recommend you not to use such features because it makes code very hard to understand and modify.
Keep it simple and you will avoid getting in troubles.
Life is a stage and we are all actors!
|
|
|
|
|
Hristo Bojilov wrote: Keep it simple
I agree.
BTW: the exact order of the expression evaluation and the side-effects has been undefined in many languages for a long time. However IMO a compiler should issue a warning whenever you rely on assumptions, i.e. undefined results.
|
|
|
|
|
|
Hello all
this is my maze. when the worm reach to wall it passes the wall and sometime it stops.
#include <stdio.h>
#include <conio.h>
int dir=0;
char wall[40][50]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1},
{1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,1},
{1,1,0,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0,0,1,1,0,1},
{1,0,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,1,1,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0,0,1,1,0,1},
{1,0,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
create_wall();
arrow_keys();
void main()
{
int a=3,b=2;
create_wall();
lbl : arrow_keys();
if (dir==72){
b-=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else b+=1;goto lbl;
}
if (dir==75){
a-=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else a+=1;goto lbl;
}
if (dir==77){
a+=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else a-=1;goto lbl;
}
if (dir==80){
b+=1;
if (wall[a][b]==0){wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
else b-=1;goto lbl;
}
if (dir==0) {printf("exit");}
}
create_wall(){
clrscr();
int i,j;
for(i=0;i<15;i++){
for(j=0;j<26;j++){
if (wall[i][j]==1) printf("²");
else printf(" ");
}
printf("\n");
}
gotoxy(2,2);
printf("**");
}
int arrow_keys()
{
int ch;
while(1)
{
ch=getch();
if(ch==0)
{
ch=getch();
if(ch==72) {dir=72; break;}
if(ch==75) {dir=75; break;}
if(ch==77) {dir=77; break;}
if(ch==80) {dir=80; break;}
}
else dir=0;
break;
}
}
Where is the problem.
|
|
|
|
|
Here are a few ideas for you to make life easier in the future:
1. Don't use global variables when you don't have to. In your case, wall might be acceptable, but dir should not be global.
2. Don't confuse others by writing wall[40][50] and only initialize 15/26.
3. If wall is global, add globals for the wall bounds as well. You don't want to use hard coded values (like 15 and 26) anywhere else but very close to you wall definition. (you can't trust sizeof() operator either the way you have done it)
4. Be consistent in your naming. Don't use indexes a and b in one place, and i and j in another for the same thing. x and y would do just fine.
5. Do not use labels and goto where a simple loop will be enough. Your code will be hard to follow and maintain, even if it's correct. If you need a label, and it will be a long time before you do, give it a meaningful name.
6. Let arrow_keys return its value instead of changing a global variable. It's defined to return an int, but never does. So is also create_wall() by the way.
7. Use constant identifiers instead of hard coded numbers. An enum type is well suited for your direction variable. Again, think of naming.
8. printf("%c", ...)? Have a look at putc().
9. Indentation looks a bit random, and so does placement of curly brackets. Adopt a consistent style.
That said, finding these kinds of bugs is an easy job if you step through the code in a debugger, inspecting indexes into your wall matrix.
But clean up your code first.
|
|
|
|
|
Hello all
I am working on maze in turbo c(command prompt not windows). I write 90% of it.
but there are 2 problems I do any work can't solve it.
1- When worm(** is my worm) reach a wall it don't stop. it can pass the wall?
2- the worm make longer but it should be ** in any time?
#include <stdio.h>
#include <conio.h>
int dir=0;
char wall[40][50]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1},
{1,1,0,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,1},
{1,1,0,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0,0,1,1,0,1},
{1,0,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1},
{1,1,1,1,1,1,1,1,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1},
{1,1,0,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1},
{1,1,1,1,1,0,0,0,1,1,1,0,0,0,0,1,1,1,1,0,0,0,1,1,0,1},
{1,0,1,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
create_wall();
arrow_keys();
void main()
{
int a=3,b=2;
create_wall();
lbl : arrow_keys();
if (dir==72){b-=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==75){a-=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==77){a+=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==80){b+=1; wall[a][b]='*';gotoxy(a,b); printf("%c",wall[a][b]);goto lbl;}
if (dir==0) {printf("exit");}
}
create_wall(){
clrscr();
int i,j;
for(i=0;i<15;i++){
for(j=0;j<26;j++){
if (wall[i][j]==1) printf("²");
else printf(" ");
}
printf("\n");
}
gotoxy(2,2);
printf("**");
}
int arrow_keys()
{
int ch;
while(1)
{
ch=getch();
if(ch==0)
{
ch=getch();
if(ch==72) {dir=72; break;}
if(ch==75) {dir=75; break;}
if(ch==77) {dir=77; break;}
if(ch==80) {dir=80; break;}
}
else dir=0;
break;
}
}
Please help me to solve the problems.
Thanks in advance
|
|
|
|
|
|
You're not erasing where the "worm" is when you draw the new one on the screen and you're not testing where the new worm head is and ending the game (if that's the rule) when the worms head hits a wall. To get round these problems:
- You can deal with the extra drawing by overwriting the worm's current position with spaces after you poll the keyboard but before you change the position of the worm.
- When you change the worm's position do a check to see if the new position overwrites a 1 in your maze. If it does, end the game.
Cheers,
Ash
|
|
|
|
|