-2

I am coming back to C++ after a long venture in Java world, and am regrasping the concept/syntax of how C++ uses pointers.

So a couple of examples.

in java

    public void MyFunc(MyClass class) 
    {
        class.DoSomething;
    }

is the same as

    void MyFunc (MyClass* class)
    {
        class->DoSomething;
    }

and

    public MyClass GetMyClass()
    {
        return class;
    }

is the same as (looking at it i realize i would have to store what im returning here in a pointer so its not the same...)

    MyClasss* GetMyClass()
    {
        return (&class); // could you say here "return <-MyClass"
    }

instead maybe this is the same (it would seem this should return an object of MyClass that is located at "&MyClass", so you would be able directly edit this object at this location from wherever this is returning to )

    MyClasss GetMyClass()
    {
        return (&class);
    }

also you can use -> to access the object stored at the address the pointer points to, can you use <- to store the address?

finally, what is the point of

    void MyFunc(MyClass1 &class1, MyClass2 &class2)

vs

    void MyFunc(MyClass1* class1, MyClass* class2)

is it that for the first you are passing in the data store at the address while in example 2 you are passing in the address of the data you would like to use?

WIllJBD
  • 6,144
  • 3
  • 34
  • 44
  • 1
    `MyClass` is the name of a class, not a variable. – Ed S. Sep 20 '12 at 00:24
  • I know this, put it there like that to show you it was an instance of it, hold on ill edit it for you... – WIllJBD Sep 20 '12 at 00:32
  • Java is pass-by-value, you know. – nullpotent Sep 20 '12 at 00:35
  • 2
    You guys are taking this way to literally. Calm down and stop down voting. I showed i have put thought into this, and I asked valid questions, in a way i needed them answered so I could understand. Apparently, since there have been 5 up-votes in addition to the 8 down votes, other people wanted it explained as well. So those of you down voting are contributing to a negative community. – WIllJBD Sep 20 '12 at 00:38
  • 2
    @iccthedral It's worth clarifying that although Java is pass-by-value, all object names are actually references/pointers to objects, so Java kind of automatically passes objects by reference. – 1'' Sep 20 '12 at 00:43
  • The hate is to strong here, votes to close a real question saying it is not a real question? have you not read the faq? thank you for your negative contributions to the community. – WIllJBD Sep 20 '12 at 00:46
  • @WIllJBD: I didn't downvote, but I voted to close. Things like `return class;`, `return (&class);`, etc. do not make any sense. `class` is a keyword in C++ and you cannot return something that does not exist, please **compile** all your examples to make sure they work before you ask a question. You will probably answer a lot of your questions that way and also will give meaning to your question (looking at incoherent code does not help anyone). – Jesse Good Sep 20 '12 at 01:15
  • obviously it is a reserved word, and obviously I am not trying to compile this, obviously it is to show you that i am returning an instance of the class "MyClass". Before I had it read MyClass to show it was an instance of MyClass, and there were complaints, now you are complaining about this? If you can not figure out that "class" is there to mean "instance of the object that is associated logically with it"... well, it's kind of sad. and this is not to be compiled. Why would I compile that? no one would. If you can not get this, or do not understand it either ask for clarification or move on – WIllJBD Sep 20 '12 at 01:34
  • as for the looking at incoherent code, that code is indeed coherent, it is much more self explanatory then running around using names like foo, or sdfuihwfuihsdfwdfgu, that people make up on a whim and leave the reader to ask, where did that come from, why is it there, and what does it mean? – WIllJBD Sep 20 '12 at 01:36
  • @WIllJBD: I see your point, however, I disagree with you (I expect the syntax to be (somewhat) correct). If you want get other opinions about this issue whether the closing of your question was justified or not, I recommend asking a question on meta stack overflow. – Jesse Good Sep 20 '12 at 01:48
  • I attempted to write answer, but I agree that there is no clear question here. None of the given code compiles. There are lots of people that are willing to help you, @WIllJBD. We just ask you to follow a few guidelines. I suggest that you check out [What did you try?](http://www.whatdidyoutry.com) and [SSCCE](http://www.sscce.org) for some suggestions about asking "acceptable" questions. – Code-Apprentice Sep 20 '12 at 02:19
  • To summarize, you are more likely to get help here if you ask a specific, clear-cut question with code that reproduces the problems you encounter or clearly illustrates the concept that you are asking about. – Code-Apprentice Sep 20 '12 at 02:27

3 Answers3

3

A pointer is a memory address where you can find your data (which can be an object, struct or primitive type). When you say return (&object), you're returning a memory address - a single number (say, 1736cbfa). With that memory address, you can access individual elements of object using the -> operator. You're not "pushing back" anything to the calling function, so you don't need any "<-" operator. Just think of the function as returning a number, but a special kind of magic number that tells you where the data you need is, and you should be fine.

Regarding your final point, void MyFunc(MyClass * object) sends a copy of the memory address/pointer/magic number where you find the data into the function. void MyFunc(MyClass & object) basically does the same thing behind the scenes, but allows you to refer to object1 directly (as in object.element instead of object->element). This second method is preferred when you want to modify object1 inside the function since you can't accidentally dereference a null pointer this way.

1''
  • 26,823
  • 32
  • 143
  • 200
1

Java reference types are more like C++ references than C++ pointers, so I have to disagree with almost all your claims about similar code. I would rewrite them as follows:

Java:

    public void myFunc(MyClass c) 
    {
        c.doSomething();
    }

C++:
void myFunc (MyClass& class) { class.doSomething(); }

As for your second example, this won't even compile in Java: public MyClass GetMyClass() { return MyClass; }

I think you mean something like

    public MyClass getMyClass()
    {
        return new MyClass();
    }

which in C++ can be done exactly the same way, but with a pointer instead of a reference:

    MyClass* getMyClass()
    {
        return new MyClass();
    }

As for your final example/question, let's simplify it to just one parameter:

void myFunc(MyClass c);

vs

void myFunc(MyClass & rc)

vs

void MyFunc(MyClass* pc)

Here c is an object, rc is a reference to an object, and pc is a pointer to an object.

These all have different uses. The syntax for using c and rc is similar; both use the . operator to access members, just like in Java. The syntax for using pc is different; pointers use the * operator for indirection and the -> operator for member access.

I'm not sure what else to add here unless you have a more specific question. I suggest you start refreshing your C++ skills by reviewing classes and standard object variables. When you get a handle on those, then you can move on to pointers and references.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thankyou, the one instance you said would not compile in java, was meant to show that i was using an instance of MyClass. So it should have read "return class;" – WIllJBD Sep 20 '12 at 00:43
  • 1
    @WIllJBD Since `class` is a reserved word in Java, you need to use a different variable name. I get the idea, though. – Code-Apprentice Sep 20 '12 at 01:40
1

Assuming:

class MyClass 
{
  public:
    void Foo() {}
}

class OtherClass:
{
  public:
    MyClass GetValue() { return myClass; }
    MyClass& GetRef() { return myClass; }
    MyClass* GetValPtr() { return &myClass; }
  private:
    MyClass myClass;        
}

OtherClass bar;

The first method will return a copy of the member named myClass. If you receive the variable and change it, it will not affect the member variable. You can call the Foo method like so, bar.GetVal().Foo(). Note that you have retrieved the MyClass instance by copying it. Unless you assign it to a variable you won't be able to re-use this particular instance. This is NOT calling Foo on the MyClass member instance inside bar

The second method will return a reference to the member variable. References are used because unlike pointers, they cannot be NULL. You can access the Foo method like so, bar.GetRef().Foo(). Remember that this will call the method of the MyClass member instance inside bar.

The third method will return a pointer to the member myClass. You can access the Foo method like so, bar.GetPtr()->Foo(). Remember that this will call the method of the MyClass member instance inside bar.

Aesthete
  • 18,622
  • 6
  • 36
  • 45