Click here to Skip to main content
15,909,742 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionMessage Removed Pin
18-Sep-09 5:02
professionalN_tro_P18-Sep-09 5:02 
AnswerRe: CUDA Anyone? Pin
zhao,kaiyong24-Sep-09 9:02
zhao,kaiyong24-Sep-09 9:02 
AnswerRe: CUDA Anyone? Pin
johnthomaswarner19-Feb-10 10:26
johnthomaswarner19-Feb-10 10:26 
QuestionHow to get rid of warning message from Microsoft code Pin
transoft18-Sep-09 4:57
transoft18-Sep-09 4:57 
AnswerRe: How to get rid of warning message from Microsoft code Pin
Richard MacCutchan18-Sep-09 5:21
mveRichard MacCutchan18-Sep-09 5:21 
AnswerRe: How to get rid of warning message from Microsoft code Pin
Michael Dunn18-Sep-09 8:38
sitebuilderMichael Dunn18-Sep-09 8:38 
AnswerRe: How to get rid of warning message from Microsoft code Pin
Richard MacCutchan18-Sep-09 22:17
mveRichard MacCutchan18-Sep-09 22:17 
Questionsome questions about "reference to pointer" Pin
lhyblue18-Sep-09 3:57
lhyblue18-Sep-09 3:57 
I'm recently learning C#. In C# you can pass parameters by value or by reference, and as you know, there are value types and reference types in C#.

I don't know if reference in C# means the same thing as in C++. But I think passing a reference type by reference in C# is similar to passing a pointer "by reference" in C++ (Reference to Pointer). As in both cases, the callee can change the values of the object’s state data as well as the object the ref type/pointer is referencing/pointing to.

What do you think?

I've written a program in C++ using "Reference to Pointer".


#include <iostream>
#include <string>
using namespace std;
 
class Person
{
private: 
    string name;
 
public:
    Person(string s)
    {
        name = s;
    }

    Person()
    {
        name = "";
    }

    void SayHello()
    {
        cout << "Hello, my name is " << name << "!" << endl;
    }
};
  
void Swap(Person *& a, Person *& b)
{
    Person *temp;
    temp = a;
    a = b;
    b = temp;
}
 
int main()
{
    Person *p1 = new Person("Alice");
    Person *p2 = new Person("Bob");
 
    cout << "Before swapping:" << endl;
    p1->SayHello();
    p2->SayHello();

    cout << endl << "Memory address of *p1:" << p1 << endl;
    cout << "Memory address of *p2:" << p2 << endl;
 
    Swap(p1, p2);

    cout << endl << "After swapping:" << endl;
    p1->SayHello();
    p2->SayHello();

    cout << endl << "Memory address of *p1:" << p1 << endl;
    cout << "Memory address of *p2:" << p2 << endl;
 
    delete p1;
    delete p2;
    return 0;
}


This program swaps the objects that p1 and p2 points to by passing them to Swap() by reference.

However, if I define Swap() like this:
void Swap((Person *) &a, (Person *) &b)

the compiler would display some error messages.

Swap.cpp(27) : error C2065: 'a' : undeclared identifier
Swap.cpp(27) : error C2065: 'b' : undeclared identifier
Swap.cpp(28) : error C2448: 'Swap' : function-style initializer appears to be a function definition
Swap.cpp(47) : error C3861: 'Swap': identifier not found

I really don't know why there would be errors. Why are those two definitions different?

Finally, I wonder if you know how to do the same swapping job in Java. As you know, in Java I can only pass parameters by value. Do you have any solutions?
AnswerRe: some questions about "reference to pointer" [modified] Pin
«_Superman_»18-Sep-09 4:08
professional«_Superman_»18-Sep-09 4:08 
GeneralRe: some questions about "reference to pointer" Pin
CPallini18-Sep-09 4:17
mveCPallini18-Sep-09 4:17 
GeneralRe: some questions about "reference to pointer" Pin
«_Superman_»18-Sep-09 4:19
professional«_Superman_»18-Sep-09 4:19 
GeneralRe: some questions about "reference to pointer" Pin
lhyblue18-Sep-09 4:44
lhyblue18-Sep-09 4:44 
GeneralRe: some questions about "reference to pointer" Pin
Stuart Dootson18-Sep-09 4:48
professionalStuart Dootson18-Sep-09 4:48 
GeneralRe: some questions about "reference to pointer" Pin
CPallini18-Sep-09 11:14
mveCPallini18-Sep-09 11:14 
GeneralRe: some questions about "reference to pointer" Pin
Stuart Dootson18-Sep-09 12:40
professionalStuart Dootson18-Sep-09 12:40 
GeneralRe: some questions about "reference to pointer" Pin
CPallini18-Sep-09 13:15
mveCPallini18-Sep-09 13:15 
GeneralRe: some questions about "reference to pointer" Pin
Stuart Dootson18-Sep-09 13:28
professionalStuart Dootson18-Sep-09 13:28 
GeneralRe: some questions about "reference to pointer" Pin
CPallini19-Sep-09 0:26
mveCPallini19-Sep-09 0:26 
GeneralRe: some questions about "reference to pointer" Pin
Stuart Dootson19-Sep-09 7:25
professionalStuart Dootson19-Sep-09 7:25 
GeneralRe: some questions about "reference to pointer" Pin
CPallini19-Sep-09 22:06
mveCPallini19-Sep-09 22:06 
GeneralRe: some questions about "reference to pointer" Pin
Stuart Dootson19-Sep-09 22:15
professionalStuart Dootson19-Sep-09 22:15 
QuestionDISP_E_UNKNOWNNAME Pin
Mohanraj D18-Sep-09 3:20
Mohanraj D18-Sep-09 3:20 
AnswerRe: DISP_E_UNKNOWNNAME Pin
Stuart Dootson18-Sep-09 3:25
professionalStuart Dootson18-Sep-09 3:25 
AnswerRe: DISP_E_UNKNOWNNAME Pin
CPallini18-Sep-09 3:30
mveCPallini18-Sep-09 3:30 
QuestionHow to use ShellStyle.dll in MFC Application Pin
themilan18-Sep-09 2:37
themilan18-Sep-09 2:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.