|
jharn wrote: When you say please reformat the code I am not sure what you are suggesting.
Your code snippet is barely readable, since all special characters are escaped and the formatting is not kept. Check the forum guidelines (the message "How to ask a question" on top of this message board). The point nr 7 explains exactly what you have to do when you want to paste a code snippet.
|
|
|
|
|
I think what he means is that you should use the formatting that this forum provides, either by marking your code block and clicking on the beige-underlined formatting button titled code block , or by manually surrounding your code block with the tags <pre> and </pre> . It should then look something like this in your editor window:
<pre>if (a) {
b = 1;
}
else {
b = 0;
}<pre>
(that last <pre> should have been </pre> instead actually, but that would have prematurely ended my example since I used the same tags to format it in the first place )
|
|
|
|
|
jharn wrote:
double p = (arg * 0.0068947572);
I think the above line should read:
double p = (*arg * 0.0068947572);
I must get a clever new signature for 2011.
|
|
|
|
|
I've come across a situation where I don't understand entirely what's happening regarding references (not pointers).
If we did this for example:
int someNumber = 5;
int& get()
{
return someNumber;
}
void main()
{
int a = get();
get() = 52;
int b = get();
}
We can change the value of some number by setting the return value of get() (or the long way around int& ref = get(); ref = 52; )
But, in this case I'm not sure what exactly is going on:
class bclass
{
public:
bclass(){}
virtual void cheese()=0;
bclass(const bclass& other){ _asm{int 3} }
};
class dclass : public bclass
{
public:
dclass(){val=5;}
void cheese() { _asm{int 3} }
dclass(const dclass& other){ _asm{int 3} }
int val;
};
dclass base;
bclass& get()
{
return base;
}
void main()
{
dclass argh;
argh.val = 2;
get() = argh;
get().cheese();
}
What happens when we try to set the return value of get() ? Why doesn't anything happen? I also tried it with just references to bclass so simply trying to set a reference to a bclass to a reference of a different bclass and still nothing.
Also, before anybody says anything I'm not actually trying to set the return value to something else, or set any other variables in some bizarre manner it's just something I've come across quite by accident.
EDIT: Or a much simpler example:
bclass& argh = d2();
bclass& hmm = dclass();
hmm = argh;
hmm.cheese();
Where dclass's implementation of cheese is called
|
|
|
|
|
When you assign argh to get(), you are calling an assignment operator. I do not remember the details of its interaction with the copy constructor, but the code fragment below triggers a breakpoint.
class bclass
{
public:
bclass(){}
virtual void cheese()=0;
bclass(const bclass& other){ _asm{int 3} }
bclass& operator= (bclass const& f) { _asm{int 3} } };
|
|
|
|
|
What you're trying to do is reassign the reference, which you can't do. It's a common mistake to make. What is actually occurring is the operator= is being called, but since you don't supply an implementation, it calls the compiler generated one. The compiler generated one essentially does nothing (EDIT: in this case).
For primitive values, like in your first example, works like you would think because it's just transferring values.
You can "reassign" the reference like this:
&get() = &argh;
"If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
modified on Thursday, January 27, 2011 1:59 PM
|
|
|
|
|
&get() = &argh ...
I understand what you mean but ... is that legal C++ or just a hack around a (sort of) compiler bug?
There is nothing like that in the c++ reference book!
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Just to clear up some of the confusion:
To the compiler, a reference is equivalent to a pointer in everything but the method of dereferencing. It treats the reference as a pointer to a memory location that occupies the contents of some variable.
To the programmer, a reference is equivalent to an alias. Through that reference you can manipulate the contents of another variable without knowing it's orginal name, by using an alternate name. Your method get() effectively returns a temporary alternate alias to your globale variable. You can assign this to a variable of type reference to bclass (bclass&) in order to use that variable as an alias to your global variable, or, like you did, you can just use the temporary alias immediately.
ahmed's suggestion was that you can reassign the alias to another variable, in this case the local variable argh. But it wouldn't work as the address of operator itself is not a reference, and therefore get() is not an l-value. Reassigning a variable of type reference to another variable is not possible.
|
|
|
|
|
no, it's not legal, it was just an attempt to clarify what he was attempting to do.
"If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
|
|
|
|
|
ahmed zahmed wrote: The compiler generated one essentially does nothing.
It does copy all members of the class by using each members assignment operator, default or explicitly declared and implemented.
|
|
|
|
|
Since bclass's oeprators are being called, only members of bclass are being copied, i. e. nothing. See my edit on my previous response.
|
|
|
|
|
It was more of a clarification of what seemed to be a general statement that could be interpreted as "a default assignment operator does nothing".
|
|
|
|
|
I see. I didn't read it as a general statement, but I now recognize it might be considered as such. Good point.
|
|
|
|
|
You do make a good point, so I edited my response to clarify.
Thanks for keeping me honest.
"If your actions inspire others to dream more, learn more, do more and become more, you are a leader." - John Quincy Adams
|
|
|
|
|
get() = argh; calls bclass::operator= which doesn't exist*. Even if it did exist, val is not a member of bclass and thus couldn't be copied anyway. If you define the return type of get() as dclass&, and in addtion to that define the assignment operator for this class, then it might work.
P.S.: to my knowledge assignment operators can't be made virtual, but you might be able to get this to work with a trick like this:
class base {
virtual void assign(const base& other)=0;
public:
base& operator=(const base& other) { assign(other); return *this; }
};
class derived : public base {
virtual void assign(const base& other) {
const derived* pOther = &(dynamic_cast<const derived&>(other));
if (pOther != 0) {
val = pOther->val;
}
}
public:
derived() : val(5) {}
int val;
};
derived myglobalderived;
derived& get() { return myglobalderived; }
void test() {
derived mylocalderived;
mylocalderived.val = 3;
get() = mylocalderived;
std::cout << "get() = " << get().val;
}
*P.P.S: while bclass::operator= hasn't been defined explicitely, the compiler might create one automatically (but it wouldn't make any difference as explained above). I am not sure in this case however, since assignment operators can't be virtual, and bclass is an abstract class. A member-wise copy will not make any sense, as you can't create instances for an abstract class, and derived classes will overwrite, not override the assignment operator.
modified on Thursday, January 27, 2011 5:58 AM
|
|
|
|
|
My TCP/IP program transfers large data between client/server frequently.
I want to compress data to reduce transferring amount and increase transferring speed.
So I need simple, fast and efficient source code to compress/decompress data, winzip is too slow because it considers many things, which are useless for me, such as file structure.
Do you know any fast compressing source code other than winzip?
Thanks for links or suggestions.
|
|
|
|
|
The only thing I know of is zlib, which also uses the deflate algorithm to compress data. But really the thing that gives you the most control over the speed of the compression is how highly compressed you want it. I think zlib gives you a range of 1-9 for speed vs compression where 1 is the fastest but results in the largest size and 9 gives the smallest size but at the cost of time.
I think though, that if you do some quick checks on the data you want to compress then you can save some time yourself, JPEGs and PNGs are already compressed for example so it would be a waste of time to try and compress that data, and you would be better to just send it as-is.
|
|
|
|
|
|
I have two different applications appDad and appKid. appDad is a Windows Forms Application and it calls appKid.exe. appKid does a series of prinf´s that I want to display in a text box in appDad.
Some suggest that I use FILE * output = _popen ("ls", "r") or freopen ("/dev/tty", "w", stdout) but I´m not quite sure what I´m doing.
Am I being lazy for asking instead of researching a bit more? I hope not...
|
|
|
|
|
See here and here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Ok, thanks a lot. But... is there a simpler way to do it in .NET?
|
|
|
|
|
piul wrote: But... is there a simpler way to do it in .NET?
.NET is a framework, not a language. If you are referring to Managed C++, see here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
How to get a CBitmap from a LPDIRECTDRAWSURFACE7 ?
modified on Thursday, January 27, 2011 10:10 AM
|
|
|
|
|
Try something like this:
1. Use IDirectDrawSurface7::GetSurfaceDesc[^] to get the dimensions of the surface.
2. Use IDirectDrawSurface7::GetDC[^] to get a device context for the surface
3. Use CBitmap::CreateCompatibleBitmap[^], specify the DC you got from the surface, you might use CDC:Attach[^] to get a CDC from the HDC, just don't forget to also Detach it before releasing the DC. The dimensions for your bitmap you got in step 2.
4. Create a DC compatible with the surface DC using CDC::CreateCompatibleDC[^]
5. Select the bitmap you created in step 3 into the DC you created in step 4. Do a bit-block-transfer with CDC::BitBlt[^] to move the pixel data from the DC you got in step 2 to the DC you got in step 4.
6. Deselect the bitmap (3) from the DC (4), destroy the DC (4)
7. Use IDirectDrawSurface7::ReleaseDC[^] to free up the DC you got in step 2.
8. Live happily ever after.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> //TODO: Implement signature here<
|
|
|
|
|
Hi thx for the awnser.
I haven't got it to work yet a get an assertion error when calling CreateCompatibleBitmap()
Not sure I do the right thing when geting CDC object.
DDSURFACEDESC2 SurfaceDesc;
m_pDrawSurface->GetSurfaceDesc(&SurfaceDesc);
HDC sourceHDC = NULL;
m_pDrawSurface->GetDC(&sourceHDC);
CDC *pCDC = CDC::FromHandle(sourceHDC);
pCDC->Attach(sourceHDC);
CBitmap *cBmp;
cBmp->CreateCompatibleBitmap(pCDC,SurfaceDesc.dwWidth, SurfaceDesc.dwHeight);
CDC dcMemory;
dcMemory.CreateCompatibleDC(pCDC);
dcMemory.SelectObject(cBmp);
dcMemory.BitBlt(0,0,SurfaceDesc.dwWidth,SurfaceDesc.dwHeight,pCDC,0,0,SRCCOPY);
pCDC->Detach();
dcMemory.Detach();
dcMemory.DeleteDC();
m_pDrawSurface->ReleaseDC(sourceHDC);
|
|
|
|