|
Hello,
I have the following problem. I want to create a temporary object that is transparent to the caller of the function which creates the temporary. We all know that a temporary object is created on the stack for objects returned by value from a function.
I wan't to use this property of the temporary to create a locking / unlocking mechanism for an object. See the following code for details
class CArbitraryClass
{
public:
void DoFoo() {}
};
class CWrapperClass
{
public:
CInvisibleTemporary operator->()
{
return CInvisibleTemporary::CInvisibleTemporary();
}
};
class CInvisibleTemporary
{
public:
CArbitraryClass& operator->() { return m_ArbitraryObject; }
private:
CArbitraryClass m_ArbitraryObject;
};
So the code should look like the following statements:
CWrapperClass Wrapper;<br />
Wrapper->DoFoo();
But it looks like:
CWrapperClass Wrapper;<br />
Wrapper.operator ->().operator ->().DoFoo();
Am I trying to do something impossible?
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Hi, Bob,
its normal that you should write
CWrapperClass Wrapper;<br />
Wrapper.operator ->().operator ->().DoFoo();
because after each function .operator ->() call you have returned a other object
temporary, pointer or reference. From this object you can call again some functions and so on.
The first call (Wrapper.operator ->() ) returns the temp object 'TempX' (CInvisibleTemporary)
You have now a object from which you can call some functions like TempX.operator ->() which returns a reference to the encapsulted object (CArbitraryClass m_ArbitraryObject ) and now you can call the finale funtion DoFoo();
If you want to encapsulate the function call list, you will have to return the correct object immediatly. But then you could lose the transparancy you need.
Maybe this could be done by using templates. I will check this out!!
codito ergo sum
|
|
|
|
|
BadKarma wrote: its normal that you should write
CWrapperClass Wrapper;<br />
Wrapper.operator ->().operator ->().DoFoo();
I know that I should write this and I want to avoid this obfuscation by introducing the invisible temporary.
My current solution is that the user has to create the temporary himself by calling a function from the wrapper class. This is OK, but if the user doesn't destroy the object, the cache I use it for, might get polluted.
I'll let you know if I find a solution. Thanks for your time, really appreciate it!
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
You can decrease the level of complexity by changing the -> operator in the CInvisibleTemporary class
from
CArbitraryClass& operator->() { return m_ArbitraryObject; }
to
CArbitraryClass* operator->() { return &m_ArbitraryObject; }
the code for calling is now
Wrapper.operator ->()->DoFoo();
in stead of
Wrapper.operator ->().operator ->().DoFoo();
codito ergo sum
|
|
|
|
|
Thank you!
The following change did the trick:
BadKarma wrote: CArbitraryClass& operator->() { return m_ArbitraryObject; }
to
CArbitraryClass* operator->() { return &m_ArbitraryObject; }
The compiler now automatically calls the second operator->() and everything looks like you have no temporary object! I tested this syntax on VS2005 and I don't know if it works on previous versions of visual studio. I'll try it as soon when I get my hands on one of those versions.
I first tried it with implicit casting, but this didn't work. I now have the solution thanks to you.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Hi, bob
If you mean invisible (like hide the class/object from the user) instead of transparent (don't care for the object class type), I may have a solution that works like you intended.
class CArbitraryClass
{
public:
void DoFoo() {}
};
class CInvisibleTemporary
{
public:
CInvisibleTemporary(void (CArbitraryClass::*pFunction)())
{
(m_ArbitraryObject.*pFunction)();
}
private:
CArbitraryClass m_ArbitraryObject;
};
class CWrapperClass
{
public:
void Call(void (CArbitraryClass::*p)())
{
CInvisibleTemporary::CInvisibleTemporary(p);
}
};
The way you need to call it is like this:
CWrapperClass Wrapper;
Wrapper.Call(CArbitraryClass::DoFoo);
The function Call does create a temporary object of the type CInvisibleTemporary. To this object the function that needs to be called is given via its constructor.
The 'invisible' temporary object doens't exist anymore after the 'Call', which frees the locked resoruces.
I hope that this may be a solution or at least sets you on route to one
codito ergo sum
|
|
|
|
|
how can you use a class to be a utility class of another class?? need help!! im quite stuck with this one
|
|
|
|
|
in many different ways ...
class MyClass
{
class MyUtilityClass
{
};
MyUtilityClass myUtilityClass;
};
class MyUtilityClass
{
};
class MyClass
{
MyUtilityClass myUtilityClass;
};
or something else ..
maybe if you give us a hint at what you want to achieve, we would be in better position to give you a better answer.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
Hello,
If the class has a default constructor, the following approach works:
CSomeClassUtil
{
public:
static void DoFoo();
private:
static CSomeClass m_Class;
};
This will not work if the class you want to use has a constructor with parameters. The following modifications will have to be made:
CSomeClassUtil
{
public:
static void Initialize()
{ m_pClass = new CSomeClass( ); }
static void DoFoo();
private:
static CSomeClass* m_pClass;
};
With this method you have to do some initialization checking to protect yourself from using uninitialized pointers and initializing the same pointer more than once resulting in memory leaks.
If this solution is not what you are looking for, could you explain your goal more clear?
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Good day
For my application that deals with the scheduling routine i need some control
like we have in Outlook 2003 in Calendar menu:
The yellow one, with the horizontal line that shows the hour periods,
and the rectangles put on it with some text.
Have anybody done this, or know where i can find - please reply
Otherwise i have to design it from scratch
Great thanks,
Denis
P.S. reply to theden@yandex.ru
|
|
|
|
|
TheDen wrote: The yellow one, with the horizontal line that shows the hour periods,
and the rectangles put on it with some text.
There are a few of those floating around Codeproject.
Here is one that I found with a quick search DayView - An Outlook Like Appointment View[^]
DEBUGGING : Removing the needles from the haystack.
|
|
|
|
|
Can anybody tell how can i fine the number of hard drives and the drive letters associated with each drive in VC++? Also the size of each drive...
|
|
|
|
|
wasife wrote: Can anybody tell how can i fine the number of hard drives...
Use the Win32_DiskDrive WMI class.
wasife wrote: ...the drive letters associated with each drive...
Use GetLogicalDrives() .
wasife wrote: Also the size of each drive...
Use GetDiskFreeSpaceEx() .
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
You can use _getdrives() and others directories routines
Example of msdn
/* GETDRIVE.C illustrates drive functions including:
* _getdrive _chdrive _getdcwd
*/
#include <stdio.h>
#include <conio.h>
#include <direct.h>
#include <stdlib.h>
#include <ctype.h>
void main( void )
{
int ch, drive, curdrive;
static char path[_MAX_PATH];
/* Save current drive. */
curdrive = _getdrive();
printf( "Available drives are: \n" );
/* If we can switch to the drive, it exists. */
for( drive = 1; drive <= 26; drive++ )
if( !_chdrive( drive ) )
printf( "%c: ", drive + 'A' - 1 );
while( 1 )
{
printf( "\nType drive letter to check or ESC to quit: " );
ch = _getch();
if( ch == 27 )
break;
if( isalpha( ch ) )
_putch( ch );
if( _getdcwd( toupper( ch ) - 'A' + 1, path, _MAX_PATH ) != NULL )
printf( "\nCurrent directory on that drive is %s\n", path );
}
/* Restore original drive.*/
_chdrive( curdrive );
printf( "\n" );
}
Output
Available drives are:
A: B: C: L: M: O: U: V:
Type drive letter to check or ESC to quit: c
Current directory on that drive is C:\CODE
Type drive letter to check or ESC to quit: m
Current directory on that drive is M:\
Type drive letter to check or ESC to quit:
|
|
|
|
|
Hi yet again
I have an application that has a bar graph to indicate the recording volume of my soundcard. This makes use of the control of MIXERCONTROL_CONTROLTYPE_PEAKMETER to draw the meter (I am using CPeakMeter class written by Langis Pitre). I have just found, however, that my target machine's soundcard (Sound Blaster Live! 24-bit) doesnt have this control! AAAAAARRRRRGGGGGHHHHH!
Is there a way round this?
I have taken a look at "Multimedia PeakMeter Control By Ernest Laurentin", but can't work out how to link this into the microphone level from my sound card.
Any help will be greatly appreciated - I apologise for asking, but I am relatively new to VC++.
Mike
|
|
|
|
|
I develop a c++ programme and I need to call some functions of subversion, but I don't succeed in using the api, somebody would have examples to help me
|
|
|
|
|
Anehl wrote: ...functions of subversion...
What's this?
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
In fact I need to use the api subversion to obtain the projects/directories tree in the repository and other functions of this kind
|
|
|
|
|
Anehl wrote: ...I need to use the api subversion...
Which is what? Please explain what this term is.
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
SubVersion is a very good "new-ish" version control system.
it offers an API to let 3rd party application access the system.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
Maximilien wrote: SubVersion is a very good "new-ish" version control system.
Thanks. Without the proper capitalization, it looked like any other noun.
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
In the first one, excuse my english.
It seems to me that an API is an interface allowing to (re)use the code, so instead of reprogramming completely we can use this API to make the link between two codes.
|
|
|
|
|
David know what an API is. The question was regarding to the 'subversion' word.
Ok now concerning your problem, you say that you are unable to make it work. Ok, but could you be more specific ? It is really vague so it's really difficult to help you. Give as much detail (usefull details I mean ) as possible
-- modified at 10:03 Wednesday 19th October, 2005
|
|
|
|
|
Cedric Moonen wrote: David know what an API is.
...scrambles to put dictionary away...
Thanks, Cedric!
"Take only what you need and leave the land as you found it." - Native American Proverb
|
|
|
|
|
I make a gui which connects to sourcesafe or subversion database, and give directories/projects tree from the root of the repository, to allow then to select the wanted projects.
So, with subversion I manage to make nothing, I don't succeed in to compile the api, nor to use it
|
|
|
|