Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 591885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:40:55+00:00 2026-05-13T15:40:55+00:00

I’m learning C++ at the moment and though I grasp the concept of pointers

  • 0

I’m learning C++ at the moment and though I grasp the concept of pointers and references for the better part, some things are unclear.
Say I have the following code (assume Rectangle is valid, the actual code is not important):

#include <iostream>
#include "Rectangle.h"

void changestuff(Rectangle& rec);

int main()
{
    Rectangle rect;
    rect.set_x(50);
    rect.set_y(75);
    std::cout << "x,y: " << rect.get_x() << rect.get_y() << sizeof(rect) << std::endl;
    changestuff(rect);

    std::cout << "x,y: " << rect.get_x() << rect.get_y() << std::endl;
    Rectangle* rectTwo = new Rectangle();
    rectTwo->set_x(15);
    rectTwo->set_y(30);
    std::cout << "x,y: " << rect.get_x() << rect.get_y() << std::endl;
    changestuff(*rectTwo);
    std::cout << "x,y: " << rect.get_x() << rect.get_y() << std::endl;
    std::cout << rectTwo << std::endl;
}

void changestuff(Rectangle& rec)
{
    rec.set_x(10);
    rec.set_y(11);
}

Now, the actual Rectangle object isn’t passed, merely a reference to it; it’s address.
Why should I use the 2nd method over the first one? Why can’t I pass rectTwo to changestuff, but *rectTwo? In what way does rectTwo differ from rect?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:40:55+00:00Added an answer on May 13, 2026 at 3:40 pm

    There really isn’t any reason you can’t. In C, you only had pointers. C++ introduces references and it is usually the preferred way in C++ is to pass by reference. It produces cleaner code that is syntactically simpler.

    Let’s take your code and add a new function to it:

    #include <iostream>
    #include "Rectangle.h"
    
    void changestuff(Rectangle& rec);
    void changestuffbyPtr(Rectangle* rec);
    
    int main()
    {
        Rectangle rect;
        rect.set_x(50);
        rect.set_y(75);
        std::cout << "x,y: " << rect.get_x() << rect.get_y() << sizeof(rect) << std::endl;
        changestuff(rect);
        std::cout << "x,y: " << rect.get_x() << rect.get_y() << std::endl;
    
        changestuffbyPtr(&rect);
        std::cout << "x,y: " << rect.get_x() << rect.get_y() << std::endl;
    
        Rectangle* rectTwo = new Rectangle();
        rectTwo->set_x(15);
        rectTwo->set_y(30);
        std::cout << "x,y: " << rectTwo->get_x() << rectTwo->get_y() << std::endl;
        changestuff(*rectTwo);
        std::cout << "x,y: " << rectTwo->get_x() << rectTwo->get_y() << std::endl;
    
        changestuffbyPtr(rectTwo);
        std::cout << "x,y: " << rectTwo->get_x() << rectTwo->get_y() << std::endl;
        std::cout << rectTwo << std::endl;
    }
    
    void changestuff(Rectangle& rec)
    {
        rec.set_x(10);
        rec.set_y(11);
    }
    
    void changestuffbyPtr(Rectangle* rec)
    {
        rec->set_x(10);
        rec->set_y(11);
    }
    

    Difference between using the stack and heap:

     #include <iostream>
    #include "Rectangle.h"
    
    Rectangle* createARect1();
    Rectangle* createARect2();
    
    int main()
    {
        // this is being created on the stack which because it is being created in main,
        // belongs to the stack for main. This object will be automatically destroyed 
        // when main exits, because the stack that main uses will be destroyed.
        Rectangle rect;
    
        // rectTwo is being created on the heap. The memory here will *not* be released
        // after main exits (well technically it will be by the operating system)
        Rectangle* rectTwo = new Rectangle();
    
        // this is going to create a memory leak unless we explicitly call delete on r1.
        Rectangle* r1 = createARectangle();
    
        // this should cause a compiler warning:
        Rectangle* r2 = createARectangle();
    }
    
    Rectangle* createARect1()
    {
        // this will be creating a memory leak unless we remember to explicitly delete it:
        Rectangle* r = new Rectangl;
        return r;
    }
    
    Rectangle* createARect2()
    {
        // this is not allowed, since when the function returns the rect will no longer
        // exist since its stack was destroyed after the function returns:
        Rectangle r;
        return &r;
    }
    

    It should also be worth mentioning that a huge difference between pointers and references is that you can not create a reference that is uninitialized. So this perfectly legal:

    int *b;
    

    while this is not:

    int& b;
    

    A reference has to refer to something. This makes references basically unusable for polymorphic situations, in which you may not know what the pointer is initialized to. For instance:

    // let's assume A is some interface:
    class A 
    {
    public:
        void doSomething() = 0;
    }
    
    class B : public A
    {
    public:
        void doSomething() {}
    }
    
    class C : public A
    {
    public:
        void doSomething() {}
    }
    
    int main()
    {
        // since A contains a pure virtual function, we can't instantiate it. But we can    
        // instantiate B and C
        B* b = new B;
        C* c = new C;
    
        // or
        A* ab = new B;
        A* ac = new C;
    
        // but what if we didn't know at compile time which one to create? B or C?
        // we have to use pointers here, since a reference can't point to null or
        // be uninitialized
        A* a1 = 0;
        if (decideWhatToCreate() == CREATE_B)
            a1 = new B;
        else
            a1 = new C;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.