proper use of ->

This is a discussion on proper use of -> within the C++ Programming forums, part of the General Programming Boards category; i am kind of stuck with -> i know its accessing the class member but why would you make it ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    317

    proper use of ->

    i am kind of stuck with ->
    i know its accessing the class member but why would you make it a pointer and not just use .
    example 1:

    Code:
    class myclass
    {
    public:
    int t; 
    };
    
    int main()
    {
    myclass myclass;
    myclass.t = 1;
    return 0;
    }
    
    example 2:
    
    class myclass2
    {
    public:
    int t;
    };
    
    int main()
    {
    myclass2 *myclass2;
    myclass2->t = 1;
    return 0;
    }
    //////////////////////////////////////////
    what is the diference then with -> , and why do you have to
    make a pointer to use it

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    because sometimes you'll want to pass a pointer to an object into a function, or dynamically allocate memory for objects. for example, take a look at the code for the bot I'm using on our IRC channel:
    Code:
    #include <cstring>
    #include <string>
    #include <fstream>
    #include "IRC.h"
    
    int triggers(char*params,irc_reply_data*hostd,void*conn);
    
    int main()
    {
            IRC conn;
    
            conn.hook_irc_command("PRIVMSG",&triggers);
            conn.start("irc.phoenixradio.org",6667,"Cherilyn","Major_Small","Squishy","");
            conn.privmsg("nickserv","identify **********");
            conn.join("#Tech");
            conn.join("#irpg");
            conn.join("#John");
            conn.privmsg("irpg-bot","login Major_Small **********");
            conn.message_loop();
    
            return 0;
    }
    
    int triggers(char*params,irc_reply_data*hostd,void*conn)
    {
            IRC* irc_conn=(IRC*)conn;
    
            if(strcmp(hostd->target,"#Tech"))
            {
                    return 0;
            }
    
            if(!strcmp(params,":!triggers"))
            {
                    std::fstream file("triggers.dat");
    
                    std::string trigger;
                    std::string desc;
                    std::string line;
    
                    irc_conn->notice(hostd->nick,"\002\00315Here's a list of triggers I provide in this channel:\002\003");
    
                    while(file>>trigger)
                    {
                            getline(file,desc,'\n');
                            line="\002\0037"+trigger+"\002\00314"+desc+"\003";
                            irc_conn->notice(hostd->nick,line.c_str());
                    }
    
                    irc_conn->notice(hostd->nick,"\002\00315If you have any questions about me or my triggers, contact Major_Small\002\003");
    
                    file.close();
            }
            else if(!strcmp(params,":!new"))
            {
                    irc_conn->notice(hostd->nick,"\00314Welcome.  Please register your name with nickserv by typing \0037/msg nickserv register <password> <email> \00314.  If you're using the Java Chat and haven't taken a name, Please do so before registering by typing \0037/nick <newnick>\003");
            }
            else if(!strcmp(params,":!languages"))
            {
                    std::fstream file("languages.dat");
                    std::string line;
                    getline(file,line,'\n');
                    line="\00314We have people who can help you with "+line+" and more";
                    irc_conn->notice(hostd->nick,line.c_str());
            }
            else if(!strcmp(params,":!paste"))
            {
                    irc_conn->notice(hostd->nick,"\00314Use \0037http://rafb.net/paste \00314to paste your code\003");
            }
            else if(!strcmp(params,":!userlist"))
            {
                    std::string founder;
                    std::string admin;
                    std::string ops;
                    std::string hops;
                    std::string voice;
                    std::string bots;
    
                    std::fstream file("users.dat",std::ios::in);
                    getline(file,founder,'\n');
                    getline(file,admin,'\n');
                    getline(file,ops,'\n');
                    getline(file,hops,'\n');
                    getline(file,voice,'\n');
                    getline(file,bots,'\n');
                    file.close();
    
                    irc_conn->notice(hostd->nick,"\002\00315Here's a list of the users with access levels on #Tech:\003");
                    founder="\002\0037Founder:\002          \00314"+founder+";\003";
                    irc_conn->notice(hostd->nick,founder.c_str());
                    admin=  "\002\0037Admin:\002            \00314"+admin+";\003";
                    irc_conn->notice(hostd->nick,admin.c_str());
                    ops=    "\002\0037Operators:\002        \00314"+ops+";\003";
                    irc_conn->notice(hostd->nick,ops.c_str());
                    hops=   "\002\0037Helper-Operators:\002 \00314"+hops+";\003";
                    irc_conn->notice(hostd->nick,hops.c_str());
                    voice=  "\002\0037Voiced Users:\002     \00314"+voice+";\003";
                    irc_conn->notice(hostd->nick,voice.c_str());
                    bots=   "\002\0037Bots:\002             \00314"+bots+";\003";
                    irc_conn->notice(hostd->nick,bots.c_str());
                    irc_conn->notice(hostd->nick,"\002\00315If you're not on this list, see !new.  If you've registered and haven't been given an access level, ask Major_Small.  If you've been given an access level but still aren't in this list, just wait until you're added\003");
            }
            else if(!strcmp(params,":!channels"))
            {
                    std::fstream file("channels.dat",std::ios::in);
                    std::string chan;
                    std::string desc;
    
                    irc_conn->notice(hostd->nick,"\002\00315This is list of other channels on our network you may also find interesting:\002\003");
    
                    while(file>>chan)
                    {
                            getline(file,desc,'\n');
                            chan="\002\0037"+chan+"\002\00314"+desc+"\003";
                            irc_conn->notice(hostd->nick,chan.c_str());
                    }
    
                    irc_conn->notice(hostd->nick,"\002\00315Please Enjoy your time on the network.  If you have another channel you'd like to add, Contact Major_Small\002\003");
            }
    
            return 0;
    }
    Last edited by major_small; 11-24-2005 at 04:45 PM. Reason: removing passwords >.<
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Anddos
    i am kind of stuck with ->
    i know its accessing the class member but why would you make it a pointer and not just use .
    example 1:

    Code:
    <snip>
    int main()
    {
    myclass2 *myclass2;
    myclass2->t = 1;
    return 0;
    }
    //////////////////////////////////////////
    what is the diference then with -> , and why do you have to
    make a pointer to use it
    well that will crash at runtime to start with, but aside from that there are many reasons to use pointers, (they allow you to pass-by-reference and they can be used to loop over an array for starters).

    the -> is really a shorthand for (*ptr). it's just so much handier to type ptr->

    to give a concrete example, imagine you had a class MyClass
    Code:
    class MyClass
    {
    public:
        int massiveArrary[10000]; // note public vars generally bad
    };
    passing this function by value will be expensive (copying 40000 bytes on an int32 machine). if you pass a pointer it's much cheaper (only copying 4 bytes on most machines).

    of course you can achieve the same effect with references, which are basically syntactic sugar for pointers.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    i am kind of stuck with ->
    i know its accessing the class member but why would you make it a pointer
    Part of your question is really: "what good are pointers?". Don't worry about that for now. Learn the mechanics of how to use them, and when you learn about functions, you'll learn about "passing by value" and "passing by reference" and the differences which make pointers very efficient.

    what is the diference then with -> , and why do you have to
    make a pointer to use it
    -> is shorthand for a combination of the dereference operator(*), which gives you the thing a pointer points to, and the member access operator(.), which accesses a member of an object. Here is an example:
    Code:
    #include<iostream>
    using namespace std;
    
    class Apple
    {
    public:
    	int num;
    };
    
    
    int main() 
    {
    	Apple a;
    	a.num = 10;
    	Apple* pApple = &a;
    
    	cout<<pApple->num<<endl;
    	cout<<(*pApple).num<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 11-24-2005 at 06:52 PM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Quote Originally Posted by Anddos
    i am kind of stuck with ->
    i know its accessing the class member but why would you make it a pointer and not just use .
    example 1:

    Code:
    class myclass
    {
    public:
    int t; 
    };
    
    int main()
    {
    myclass myclass;
    myclass.t = 1;
    return 0;
    }
    
    example 2:
    
    class myclass2
    {
    public:
    int t;
    };
    
    int main()
    {
    myclass2 *myclass2;
    myclass2->t = 1;
    return 0;
    }
    //////////////////////////////////////////
    what is the diference then with -> , and why do you have to
    make a pointer to use it
    You main should be
    Code:
    myclass2 *myclass;
    myclass = new myclass2; //allocate memory for it
    myclass->t = 1;
    return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small -> big -> bigger -> bigger than bigger -> ?
    By happyclown in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-11-2009, 12:12 PM
  2. Replies: 2
    Last Post: 09-20-2006, 05:40 PM
  3. Dev-C++ -> Tools - > Editor -> Syntax
    By Yuri2 in forum C++ Programming
    Replies: 19
    Last Post: 07-03-2006, 07:48 AM
  4. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 01:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21