|
What I was referring to is that "the 'this' keyword refers to an instance of class A".
Sort of like A* this . When you then want the value of int a for that instance, you dereference the pointer and access a: this->a .
But because the compiler is smart, it'll also understand what you want if you simply write 'a' and omit the 'this->' part.
The reason I used a bit more confusing way of saying it (maybe), is that I wanted to make clear distinction between the class A and an instance of class A.
This distinction particularly comes into play when statics are involved (the telling difference is that there is no 'this' in that case, because you're not talking about instances of A when you're inside a static function using static variables).
Hope this makes it more clear. If not, feel free to ask more questions
|
|
|
|
|
Abder_Rahman wrote: ...I specifically just want to now what this refers to.
It's simply the current instance of the class.
"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
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
|
|
|
|
|
I have an Automation excel class with follow method :
CString CXLAutomation::GetCellValueCString(int nColumn, int nRow)
{
CString szValue =_T("");
if(NULL == m_pdispWorksheet)return szValue;
VARIANTARG vargRng, vargValue;
ClearAllArgs();
AddArgumentDouble(NULL, 0, nColumn);
AddArgumentDouble(NULL, 0, nRow);
if(! ExlInvoke(m_pdispWorksheet, L"Cells",&vargRng, DISPATCH_PROPERTYGET, DISP_FREEARGS))return szValue;
if(! ExlInvoke(vargRng.pdispVal, L"Value", &vargValue, DISPATCH_PROPERTYGET, 0))return szValue;
VARTYPE Type = vargValue.vt;
switch(Type)
{
case VT_UI1:
{
TCHAR nChr = vargValue.bVal;
szValue = nChr;
}
break;
case VT_I4:
{
long nVal = vargValue.lVal;
szValue.Format("%i", nVal);
}
break;
case VT_R4:
{
float fVal = vargValue.fltVal;
szValue.Format("%f", fVal);
}
break;
case VT_R8:
{
double dVal = vargValue.dblVal;
szValue.Format("%f", dVal);
}
break;
case VT_BSTR:
{
BSTR b = vargValue.bstrVal;
szValue = b;
}
break;
case VT_BYREF|VT_UI1:
{
unsigned char* pChr = vargValue.pbVal;
szValue = (TCHAR)*pChr;
}
break;
case VT_BYREF|VT_BSTR:
{
BSTR* pb = vargValue.pbstrVal;
szValue = *pb;
}
break;
case 0:
{
szValue = _T("");
}
break;
}
return szValue;
}
but haven't VT_DATE and VT_DATE | VT_BYREF case ... can you give me an help hand ?
I can show you what I try , but didn't working ...
Thank you .
P.S. I search to google , but I didn't find something appropiate ...
|
|
|
|
|
Initialize (i.e. construct) a COleDateTime object with vargValue.date and then use COleDateTime 's Format method, see COleDateTime documentation[^].
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]
|
|
|
|
|
Ok , I will try , but how VT_DATE | VT_BYREF case ?
|
|
|
|
|
What about:
COleDateTime dt(*(vargValue.pdate));
?
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]
|
|
|
|
|
Great ! Now does function well.
Thanks a lot !!!
I don't know how to test last case (VT_DATE | VT_BYREF).
|
|
|
|
|
Similar to VT_R8, except that you know the range of values. It's fairly easy to convert back and forth from Excel 2003's date time serial noting the leap year error in 1900. If using Excel 2007 or 2010, you need to know what date format is being used in Excel.
|
|
|
|
|
Hello Friends
Here is the Scenario:
#ifndef _A_H
#define _A_H
class TEST
{
int num;
void fun();
}
TEST* _pObj = new TEST();
_pObj->num++;
I want to access num in this cpp file also
How can I use the same Object in cpp file.
I tried by adding header file. And where do I declare and initialize the TEST class object pointer so that i can use it here
Thanks & Regards
Yogesh
|
|
|
|
|
You can do this by declaring TEST* _pObj as a global pointer in A.cpp.
In SAMPLE.cpp make the declaration extern TEST* _pObj globally.
This way you will be able to access the same object from both the files.
However, I think this is where you need a singleton class.
This way you will be able to access the one and only object of the class from any CPP file in which you include the A.h file.
Here are a couple of articles on singleton -
C++ Design Pattern: What is a Singleton class?[^]
Singleton Pattern & its implementation with C++[^]
|
|
|
|
|
Thx For Your Reply And I think creating Singleton is the right way to access.
But If i have SAMPLE.h then is there any way to access TEST class variable and without declaring Global Object pointer?
Regards
Yogesh
modified on Thursday, April 14, 2011 8:03 AM
|
|
|
|
|
if you have a class that's the owner/parent of both of those, he can pass the pointers to one another.
|
|
|
|
|
What is the flow of your program? Or more to the point, what is the lifecycle of your TEST object? In C/C++, when you create an object, then you have to make sure that it will be properly destroyed as well. In this case, since you call new to create your TEST object, you have to make sure that you destroy it again, using delete . Normally you ensure this either by calling delete in the same function where you call new, or, if the object is a member of another class, you call delete in the destructor of this class.
Now we come to your actual question: How can I use this object in another cpp file? I will ask back: How do you call the code in that other cpp file? (And more: how do you execute that code in A.cpp that creates your TEST object?) You somehow have to tell the computer to execute that code, and the only way to do that is by calling a function from that file. You say you don't have a header for that file though - but how then can you call any function from here?
The answer is, you will at some point, somewhere, call a function in your other cpp file - and that is assuming that wherever you call this function from, in that file you must define the prototype for the function that you call! If not by including a header (that you say does not exist), then by manually copying the prototype here.
When you do that call, just add the pointer to the parameter list of that function. That's all. Below is some sample code - I've made a few assumptions about what you did, but made sure it will work this way:
class TEST {
public:
TEST();
int num;
void fun();
}
TEST* createTest();
void destroyTest(TEST* pObj);
#include "A.h"
TEST::TEST() : num(0) {}
void TEST::fun() {}
TEST* createTest() {
TEST* pObj = new TEST();
pObj->num++;
return pObj;
}
void destroyTest(TEST* pObj) {
delete pObj;
}
#include "A.h"
void theUnknownFunction(TEST* p) {
int mynumber = p->num;
}
#include "A.h"
void theUnknownFunction(TEST* p);
int main() {
TEST* pObj = createTest();
theUnknownFunction(pObj);
destroyTest(pObj);
}
|
|
|
|
|
Ok, I really hate the answers I'm seeing from other people.
In this code:
yogeshs wrote: //CPP file A.cpp /*In this class I want to create a Pointer of object in some function*/ TEST* _pObj = new TEST(); _pObj->num++;//num already initialize in constructor.
Why is this in A.cpp? He's creating an object of his own type? Although doable, don't think that's what you're trying to accomplish.
If this object is to be used in SAMPLE.cpp, then that's where it should be initialized and accessed (now you don't have to do it this way, but I think that's what you're trying to do).
In Sample.h or Sample.cpp:
#include "A.h"
class CSample
{
public:
CSample(){m_obj = new TEST;}
~CSample(){delete m_obj;}
private:
TEST* m_obj;
};
[edit]
If I'm wrong on what you're trying to accomplish, let me know and I'll clear it up for you.
[/edit]
|
|
|
|
|
Hello Guys
Thanks For all your valuable Replies.
Dear Sir,With this solution If I initialize test variable in sample header file then it will create a new instance And I want the same instance tht i used in A.cpp
Bcoz i want to access the same variable tht i update in other file.
I tried by creating singleton class.I made the Test class as Singleton.And then If I call its instance in global variable(Test class Object) then only I cn access the same object by using extern in anoher cpp file.
Is there any other idea so that i dont need to use extern.
Any Ideas?
Regards
Yogesh
modified on Friday, April 15, 2011 3:24 AM
|
|
|
|
|
I don't quite get why none of the suggested solutions suffice for you. Don't you understand how to use them?
If you want help, we need to understand what you try to accomplish, and you have been less than clear about that. Can you please answer these questions:
- Why do you not want to use extern?
- Why do you want to create an instance of class TEST in A.cpp?
- Do you know what a class is?
- Why do you not provide a header for Sample.cpp?
- How do you even call the functions in Sample.cpp when you don't have a header?
- Have you actually successfully compiled the code you've written so far, or any part of it?
- Did you actually create a project?
Sorry if some of these questions seem inappropriate, but at this point I really don't know what level of experience and knowledge you do have. I honestly just want to determine what I do or don't need to explain.
|
|
|
|
|
Hello Stefan
i understand ur condition and i respect for your valuable comments too.
But i have the scenario like this.
I told my situation,and why i am doing this bcoz my project doesnt allow me to do other things.
Not more than info I can provide to you.
in last i m going to use by using Extern variable.
By using Extern makes the performance of my aplication low.
Thanks For all your Valuable answers and comments.
I appreciate your time for my Post.
Thanks & Regards
Yogesh
|
|
|
|
|
My questions (numbering inserted for ease of reference)
Stefan63 wrote: 1 Why do you not want to use extern?
2 Why do you want to create an instance of class TEST in A.cpp?
3 Do you know what a class is?
4 Why do you not provide a header for Sample.cpp?
5 How do you even call the functions in Sample.cpp when you don't have a header?
6 Have you actually successfully compiled the code you've written so far, or any part of it?
7 Did you actually create a project?
Your response (numbers inserted for ease of reference):
yogeshs wrote: 1. By using <layer>Extern makes the performance of my aplication low.
This answer does not make sense, as "extern" has nothing at all to do with runtime performance! You did not answer any of the other questions either, except by saying
yogeshs wrote: my project doesnt allow me to do other things What do you mean? Did you get errors?
Really, if my previous posting had been a test, you'd scored -2 out of 7 points. Fortunately for you it was not a test. But as long as you keep adding nonsensical information rather than answer easy questions, we cannot help you.
|
|
|
|
|
Great response, my 5... I think the OP has no idea what he's doing.
[edit] Or trying to do. [/edit]
|
|
|
|
|
Additionally to what others said, you can also create a method to query that one instance.
E.g:
...
TEST *GetTestInstance();
...
and
...
TEST *GetTestInstance()
{
return _pObj;
}
You can also make this a static member of TEST:
class TEST
{
int num;
void fun();
public:
static TEST *GetInstance();
};
and
...
TEST *TEST::GetInstance()
{
return _pObj;
}
> 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,
I am writing a commandline app that accepts a filename as parameter, and wonder if there is an easy way to convert the filename to a fully qualified filename when starting the app within a directory, and provide a filename that is located in the current working directory.
for example, my app is located in c:\test\myapp.exe, and the file infile.txt is located there as well. So a user can call myapp.exe infile.txt from that directory.
In my app, I'd like to be infile.txt converted to c:\test\infile.txt. Is there a function in the Win API or MFC that automatically expands filenames to full qualified path?
Next step would be to allow relativ paths as well.
Any hint how to do that?
Thanks alot.
|
|
|
|
|
I'm not entirely sure, but check if the PathResolve API does the job for you.
If not, you could maybe call GetCurrentDirectory and then append the filename to it.
|
|
|
|
|
Hi,
thanks for your hints, I have also searched in the MSDN Lib myself, and have come to the conclusion that
GetFullPathName
works best for me, since it automatically expands a given filename to the full path. That was exactly what I wanted, and it works.
|
|
|
|
|
I wrote a program using CMFCEditBrowseCtrl which had memory leaks. I identified this particular control as the issue. To confirm this, I then built a test program which just has a single CMFCEitBrowseCtrl on the dialog. It leaks upon exit.
I'm in VS2010. I used the app wizard to build a dialog based application using all the default selections. I added the CMFCEditBrowse control using the dialog editor. It compiles OK. It display the dialog and exits with memory leaks. If I remove the control, it exits ok. I can even add a CEdit control and it exits without a memory leak.
The leak is reported like this:
The thread 'Win32 Thread' (0xc3c) has exited with code 0 (0x0).
Detected memory leaks!
Dumping objects ->
{435} client block at 0x01070560, subtype c0, 212 bytes long.
a CMFCVisualManager object at $01070560, 212 bytes long
Object dump complete.
The program '[0x474] testmfcebc.exe: Native' has exited with code 0 (0x0).
Any ideas on how to proceed?
DanL
|
|
|
|
|
I just tried, and cannot reproduce the leak, using VS2010 with the latest service pack.
I create a default dialog based application with the default values, dropped a CMFCEditBrowseCtrl control to the dialog, created/added a variable for it.
build, run, quit. no leaks reported.
I tried different configuration.
Max.
Watched code never compiles.
|
|
|
|