|
That sounds like a bit more that a 'simple' window. What type of control is in each pane?
|
|
|
|
|
Hi Friends,
I came through a interesting question in inheritance. Here is the chunk of code and out put is given below.
// Inheritance tricky.cpp : Defines the entry point for the console application.
//
class Foo
{
private:
int i;
public:
Foo()
{
i = 0;
}
void geti()
{
cout << i << endl;
}
};
class Bar : public Foo
{
private:
int j;
public:
Bar()
{
j = 1;
}
void getj()
{
cout << j << endl;
}
};
void display(Foo* obj, int ctr)
{
for (int i = 0; i < ctr; i++)
{
((Foo*)obj + i)->geti();
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Foo myFoo[3];
display(myFoo, 3);
Bar myBar[3];
display(myBar, 3);
return 0;
}
and Output is
0
0
0
0
1
0
The first 3 line is 0 that's fine. But how the 2nd last is 1 ???
Is there is any way that I can get all 0 in output if I am still executing this code
Bar myBar[3];
display(myBar, 3);
PLease help me out.
Thanks in Advance.
Regards,
Amrit
-- modified 12-May-15 15:52pm.
|
|
|
|
|
myBar[1].i is still 0. The problem here is that you try to apply pointer-arithmetic to a base-class pointer. When you add 1 to Foo* obj in the loop in display(..), the pointer is increased by the size of Foo , because it's declared as a pointer to Foo . But it's actually (initially) pointing to instances of Bar , whose size is larger than that of Foo - so after adding 1 to the pointer, it points to some address still within the first instance of Bar , not to the second Bar . The output of 1 is whatever happens to be at the address where i should be if the pointer was valid.
The closest thing you can do to make it work is to use pointers to pointers:
void display2(Foo** obj, int ctr)
{
for (int i = 0; i < ctr; i++)
{
(*(obj + i))->geti();
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Foo myFoo[3];
display(myFoo, 3);
Bar myBar[3];
Foo *myBaz[3] = { &myBar[0], &myBar[1], &myBar[2] };
display2(myBaz, 3);
return 0;
}
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Hi Sascha,
I am not able to understand why we need to take address of Bar objects in line
Foo *myBaz[3] = { &myBar[0], &myBar[1], &myBar[2] };
and why we need to pass to pass the address of myBaz to a Foo** ( Double Pointer )?
How surprisingly its working fine now?
One more thing I still didn't get it hpw it prints 1 for 2nd iteration of Bar object.
If I am not wrong, even if we are passing Bar address to Foo pointer, it wil call the geti function of Foo only ? so It should print variable i value that is 0 ?
Please clear my doubt.
Thanks
|
|
|
|
|
Hi Amrit,
I'll try to explain it with some diagrams.
First, the simple case - you probably know this, but I'll show it for completeness. The class Foo has a single member of type int (int32), so it has a size of 4 bytes.
# 3 instances of Foo in memory with a Foo* pointer pointing to it:
Foo myFoo[3];
Foo* ptr = myFoo;
# ptr ptr+1 ptr+2 # (100) (100+4) (100+8)
# | | |
# v v v
# Memory address: 100..103 104..107 108..111 # Instance: --Foo1-- --Foo2-- --Foo3--
# Members: ---i---- ---i---- ---i----
Now with the class Bar (8 bytes) and your original approach:
# 3 instances of Bar in memory with a Foo* pointer pointing to it:
Bar myBar[3];
Foo* ptr = myBar;
# ptr ptr+1 ptr+2 # (100) (100+4) (100+8) # | | | # v v v
# Memory address: 100........107 108........115 116........123
# Instance: -----Bar1----- -----Bar2----- -----Bar3-----
# Members: ---i---___j___ ---i---___j___ ---i---___j___
# Value: 0 1 0
And now with pointers to pointers. Let's say we're on a 32bit-system here, then a pointer has the size of 4 bytes (32 bit). So, when doing pointer arithmetic with pointers to pointers, adding 1 means always increasing its pointed-to-address by 4 bytes. It doesn't have anything to do with the size of the class that's being pointed to by the pointer that's being pointed to
# 3 instances of Bar in memory with a Foo** pointer pointing to Bar* pointers:
Bar myBar[3]; Foo *myBaz[3] = { &myBar[0], &myBar[1], &myBar[2] };
# Value: 100 108 116
Foo** pptr = myBaz;
# pptr pptr+1 pptr+2
# (200) (200+4) (200+8) # | | |
# v v v
# Memory address: 200 204 208 # Pointer: &myBar[0] &myBar[1] &myBar[2]
# Value: 100 108 116
# | \ \__
# | \ \__
# | *pptr \ *(pptr+1) \ *(pptr+2) # v v v
# Memory address: 100........107 108........115 116........123
# Instance: -----Bar1----- -----Bar2----- -----Bar3-----
# Members: ---i---___j___ ---i---___j___ ---i---___j___
Does this answer your questions?
/Sascha
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
|
All the resource files and dlls are changed to English but cannot find the reason of displaying the Chinese language.
Peer.rc code is now that compiles fine.
#ifdef string ID IDS_ENUMSTRTEST#error Microsoft Visual C++ #endif //APSTUDIO_INVOKED
Thanks
|
|
|
|
|
How do I can split file over 4gb in fat32 in to multiple file with same name and same directory
cygwin1.dll can do this in iso9660 file system
|
|
|
|
|
You cannot, unless you create the directories across different root paths. In any directory each file name must be unique.
|
|
|
|
|
Hi guys,
I have asked this question in an interview
How to swap address of pointers of 2 variables without using a temporary variable?
As I know the arithmetic operation is not allowed between 2 pointers. So what is the trick ?
Pls help me out. Thanks in advance.
Regards,
Amrit
|
|
|
|
|
Amrit Agr wrote: Pls help me out
Perhaps it is helpful to know that the interviewer probably didn't know what they were doing in terms of interviewing.
Hopefully you were asked some other questions which were in fact more relevant.
Amrit Agr wrote: So what is the trick ?
Being interesting to see if there is in fact a correct answer. Just googling suggests that the standard C/C++ answer which, as you noted, is not in fact the same as Managed C++ makes it very unlikely that you can in fact do it without an intermediary. And if possible it would require more consideration than just whether it was a pointer or not.
|
|
|
|
|
you can use swap() method in c/c++,
is this you want?
|
|
|
|
|
XOR swap algorithm[^]
X := X XOR Y
Y := X XOR Y
X := X XOR Y
But you cannot do this with pointers as compiler does not allow XOR operation on pointers, you have to cast pointer to integer(32bit ptr to 32bit integer and 64bit ptr to 64bit integer) beforehand. This is a favourite interview question among some interviewers (who apparently read the same interview book).
|
|
|
|
|
Hi Experts,
I have a server written in C++, Client is in JAVA and middle-ware is CORBA, now we are planing to replace CORBA by web Service(SOAP or REST). we don't want to make any changes in our business logic just want to replace CORBA by web service. Can anyone give some idea how do i approach. since I googled a lot, how to write a REST or SOAP service in C++ but didn't get any lead.
Please do let me know if you seek any further info
Thanks in advance
RYK
|
|
|
|
|
I've been getting this error when I compile in my Consumer/Producer Multi-threaded program. Can someone please help me ? Here is my code
#include <iostream>
#include <stdio.h>
#include <pthread.h>
#include <assert.h>
#include<complex>
# define BSIZE 10
# define NUM_THREADS 5
using namespace std;
typedef struct {
char buf[BSIZE];
int occupied;
int nextin;
int nextout;
pthread_mutex_t mutex;
pthread_cond_t more;
pthread_cond_t less;
} buffer_t;
buffer_t buffer;
void producer(buffer_t *b, char item)
{
pthread_mutex_lock(&b->mutex);
while (b->occupied >= BSIZE)
pthread_cond_wait(&b->less, &b->mutex);
assert(b->occupied < BSIZE);
b->buf[b->nextin++] = item;
b->nextin %= BSIZE;
b->occupied++;
pthread_cond_signal(&b->more);
pthread_mutex_unlock(&b->mutex);
}
char consumer(buffer_t *b)
{
char item;
pthread_mutex_lock(&b->mutex);
while(b->occupied <= 0)
pthread_cond_wait(&b->more, &b->mutex);
assert(b->occupied > 0);
item = b->buf[b->nextout++];
b->nextout %= BSIZE;
b->occupied--;
pthread_cond_signal(&b->less);
pthread_mutex_unlock(&b->mutex);
return(item);
}
int main(int argc, char* argv[] )
{
pthread_t ptid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&ptid, &attr, producer , NULL);
}
modified 13-Apr-15 22:13pm.
|
|
|
|
|
|
Hey Guys,
I am trying to execute the following chunk of code.
int **p = (int**)new int(5);
cout << *p;
I am getting output 00000005
As per my understanding , we don't need to typecast return value from new, as it automatically does.
Like in the following statement
int *ap = new int(20);
We don't need to do that, So why I need to do in case of double pointer??
Second thing is, in cout statement, I need to put pointer deference operator once not twice. Even after that i am getting strange output like 00000005
Please someone help me.
Thanks
Amrit
|
|
|
|
|
Amrit Agr wrote: So why I need to do in case of double pointer??
Who said you do? This ought to work just fine:
int **p = new (int*);
*p = new int;
**p = 5;
cout << **p;
delete *p;
delete p;
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
here, p is a pointer to int*.that means p can point to an int pointer and that pointer will point an int type of data.
when you do this , (int**) new int (5) ; you are actually forcing (casting) p to point to an int pointer (memory address) with value 5 ..(It actually should not be done because you don't know anything about memory address 0x5 )... however and as you made p to store something(a memory address with value 5) you also allocated memory for that purpose ......
and the typecasting was necessary as first case is very different than the second case....
in the second case --- ap is an int pointer and with new command you allocated 4 bytes of memory for ap .. and ap is now pointing to that memory location and as you passed 20 in the constructor so now that memory address is storing 20....
But, p is not an int pointer ..it's a pointer to int pointer ...so it should be passed a valid memory address in the constructor call .. not an int(what you did -- by passing 5)..that's why you had to cast...
and the answer to your last question is ... p is just a pointer (it maybe a pointer to another pointer but it still is a pointer)..and when we want to know what value a pointer is pointing to we put * (deference op.) before that pointer (only once)!
Hope,you got that
|
|
|
|
|
I am a beginner in programming and have to write a solitaire program(C++) just like what we play in windows. I have no idea where or how I should start my program. I never write a game in any computer language and I have already stuck at the beginning for 2 weeks. Can you help??
btw I just need to finish the game logic and give it to my teammate to work on graphic(GUI).
now I just write a few line about checking whether the move in those 7 piles are valid or not and create 52 cards in a class by using array but cannot generate each card uniquely.
|
|
|
|
|
That is not a simple question to answer in a forum like this. You should start with a couple of classes:
Card - contains properties such as value and suit.
Player - contains a List of cards.
Some general methods:
A randomiser that deals the cards around.
Play that selects a card and figures out where it can be placed
etc.
Quite a lot to think about before you start coding, or worrying about the UI.
|
|
|
|
|
Dear all.
I am using kinect SDK2.0. Kinect sensor 2.0; visual studio 2012 & openCV 2.4.10 installed in my system.\
I am new to this kinect platform.I am looking for c++ code test some simple code. I have tested code with SDK2.0 . those are working fine. But i didnt find the code where they add images features resources . code to track object
I would like to detect object like in youtube video
<a href="https://www.youtube.com/watch?v=bSeFrPrqZ2A">object tracking using opencv</a>[<a href="https://www.youtube.com/watch?v=bSeFrPrqZ2A" target="_blank" title="New Window">^</a>]
|
|
|
|
|
Hi all,
i need to simulate mouse events using hooks.
I read the the msdn articles about hook and how they are used but I felt that they are complex!!
could any one help me or guide me on how to do the following:
- just send one mouse event to a window that is not mine. The window is a running application such as notepad, so I just need to send the right click for example to it!
thanks for any help
|
|
|
|
|
Hooking is about capturing messages that are sent ot other applications. If you want to send a message then you just need to use the SendMessage function[^]. Although you will first need to get the handle to the Window via FindWindow[^].
|
|
|
|
|
Hi Richard,,
I used the SendMessage() function many and many times, what it did is only close the handled application!!
I used also, PostMessage() and it did the same!
I used sendInput() and there is no response at all..
that's why I went to hook..
otherwise If there is a reliable way I will not go to hooking at all..
could you help in this please,,
if you need some code i will post, but if you provide me with a reliable code that can really send the mouse event to the application without moving the cursor, I will be thankful for you really
|
|
|
|