Raised This Month: $32 Target: $400
 8% 

C++ Questions


Post New Thread Reply   
 
Thread Tools Display Modes
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 01-01-2016 , 18:42   Re: C++ Questions
Reply With Quote #11

Miu also said up above:
Quote:
When you want to reference an object that already exists rather than create a new one
meaning that code like the following, seen often in HLSDK:
PHP Code:
CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE(pent); 
which just returns entity's private data (edict_t::pvPrivateData) which could be any class that derives from CBaseEntity (that's just how things work in GoldSrc). By doing that, you are not creating a new instance of CBaseEntity, you are just pointing to an existing one, allowing you to alter entity's private data.

I really can't think of any good examples right now, sorry about that, hope these explanations are good enough to make you understand the concept a little bit better.

Last edited by klippy; 01-01-2016 at 18:43.
klippy is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 01-01-2016 , 21:24   Re: C++ Questions
Reply With Quote #12

Valve's code (and anything based on it like AMXX and SM) is NOT a good reference to be learning C++ from.
It is more C-like (pointer heavy rather than references and proper const-correctness) and avoids using the C++ STL.
__________________
asherkin is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-02-2016 , 03:17   Re: C++ Questions
Reply With Quote #13

Quote:
Originally Posted by asherkin View Post
Valve's code (and anything based on it like AMXX and SM) is NOT a good reference to be learning C++ from.
It is more C-like (pointer heavy rather than references and proper const-correctness) and avoids using the C++ STL.
Oh! so that's why it focus more in using pointers because C is not powerful than C++

Quote:
Originally Posted by KliPPy View Post
Miu also said up above:

meaning that code like the following, seen often in HLSDK:
PHP Code:
CBaseEntity *pEntity = (CBaseEntity *)GET_PRIVATE(pent); 
which just returns entity's private data (edict_t::pvPrivateData) which could be any class that derives from CBaseEntity (that's just how things work in GoldSrc). By doing that, you are not creating a new instance of CBaseEntity, you are just pointing to an existing one, allowing you to alter entity's private data.

I really can't think of any good examples right now, sorry about that, hope these explanations are good enough to make you understand the concept a little bit better.
I got you in this one, thank you so much for your examples dude!
TheDS1337 is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-02-2016 , 09:29   Re: C++ Questions
Reply With Quote #14

Sorry for double post.

Being trying this code and I dont really know why it's not working... :/
PHP Code:
#include <iostream>

using namespace std;

class 
CFriends
{
public:
    
char *pName, *pNickname;    

    
CFriends() 
    { 
//        pName = new char[32];            THIS MAKES IT EVEN WORST
//        pNickname = new char[32];        THIS MAKES IT EVEN WORST
//        cout << "Allocating vars." << endl;        
    
}

    ~
CFriends() 
    {     
//        delete pName;
//        delete pNickname;
//        cout << "Deallocating vars." << endl;
    
}
};

/*                                        DOENT WORK
const char *FirstToUpperchar(char *pText)
{
    if (*pText >= 'a' && *pText <= 'z')
        pText[0] = 'A' - 'a';

    return pText;
}
*/

int main()
{
    
int friends;

    
cout << "How many friends you have ?" << endl;
    
cin >> friends;

    
CFriends *pFriends = new CFriends[friends];

//    char *name = new char;                DOENT WORK
//    char *nickname = new char;            DOENT WORK
    
char name[32], nickname[32];

    
int iterator

    for (
iterator 0iterator friendsiterator++)
    {
        
cout << "Enter friend name:";
        
cin >> name;
        
pFriends[iterator].pName name;

        
cout << "Enter his nickname:";
        
cin >> nickname;
        
pFriends[iterator].pNickname nickname;

//        pFriends[iterator].pName = name;                Tried with *name too 
//        pFriends[iterator].pNickname = nickname;        with *nickname too 
    
}

    
cout << "Your friends are:" << endl;
    
cout << "-----------------" << endl;

    for (
iterator 0iterator friendsiterator++)
    {
//        FirstToUpperchar(pFriends[iterator].pName);
//        FirstToUpperchar(pFriends[iterator].pNickname);

        
printf("%d. %s (%s)\n"iterator 1pFriends[iterator].pNamepFriends[iterator].pNickname);
    }

    
cout << "-----------------" << endl;

    
system("PAUSE");

    
delete[] pFriends;    
    return 
0;

TheDS1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 01-02-2016 , 11:30   Re: C++ Questions
Reply With Quote #15

If you are wondering why does it print same name and nickname for every friend, it's because CFriends::pName and CFriends::pNickname are just pointers, and by doing:
PHP Code:
pFriends[iterator].pName name;
// ...
pFriends[iterator].pNickname nickname
you are not copying the value of name and nickname to pName and pNickname, respectively, you are just making these 2 pointers point to these 2 arrays (name and nickname). What happens at the end of that loop is that all your CFriends (and you should've named it CFriend, not plural) objects have their pName and pNickname pointing to these 2 arrays, resulting in every friend object having the same values.
You should make CFriends::pName and CFriends::pNickname actual arrays, not just pointers, ie:
PHP Code:
char name[32];
char nickname[32]; 
Also, to copy the string, use strncpy() or any similar function. Also, your code is too "C-ish", you should just use std::string objects for strings, they will make your life much easier, but I guess you are here just learning how to use pointers correctly.
klippy is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 01-02-2016 , 11:42   Re: C++ Questions
Reply With Quote #16

PHP Code:
#include <iostream>

using namespace std;

class 
CFriends
{
public:
    
char *pName, *pNickname;

    
CFriends()
    {
        
pName = new char[32];
        
pNickname = new char[32];
        
//        cout << "Allocating vars." << endl;        
    
}

    ~
CFriends()
    {
        
delete[] pName;
        
delete[] pNickname;
        
//        cout << "Deallocating vars." << endl;
    
}
};

const 
char *FirstToUpperchar(char *pText)
{
    if (*
pText >= 'a' && *pText <= 'z')
        
pText[0] += 'A' 'a';

    return 
pText;
}

int main()
{
    
int friends;

    
cout << "How many friends you have ?" << endl;
    
cin >> friends;

    
CFriends *pFriends = new CFriends[friends];

    
//    char *name = new char;                DOENT WORK
    //    char *nickname = new char;            DOENT WORK
    
char name[32], nickname[32];

    
int iterator;

    for (
iterator 0iterator friendsiterator++)
    {
        
cout << "Enter friend name:";
        
cin >> name;
        
strcpy(pFriends[iterator].pNamename);

        
cout << "Enter his nickname:";
        
cin >> nickname;
        
strcpy(pFriends[iterator].pNicknamenickname);

        
//        pFriends[iterator].pName = name;                Tried with *name too 
        //        pFriends[iterator].pNickname = nickname;        with *nickname too 
    
}

    
cout << "Your friends are:" << endl;
    
cout << "-----------------" << endl;

    for (
iterator 0iterator friendsiterator++)
    {
        
FirstToUpperchar(pFriends[iterator].pName);
        
FirstToUpperchar(pFriends[iterator].pNickname);

        
printf("%d. %s (%s)\n"iterator 1pFriends[iterator].pNamepFriends[iterator].pNickname);
    }

    
cout << "-----------------" << endl;

    
system("PAUSE");

    
delete[] pFriends;
    return 
0;

Miu is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-02-2016 , 12:51   Re: C++ Questions
Reply With Quote #17

Thank you both for your answers! and I have to say yes I'm trying to understand the using of pointers and in same time I'm trying to use dynamic memory (that's why I didnt use std::string and I pointed *pName and *pNickname), since we don't know the exact buffer it can be bigger than 32 who know, I'm trying to get them dynamically, can you show me how can I make all of: pName, pNickname, name, nickname dynamically ? without giving them a size since putting [] instead of [32] ain't gonna work.

Last edited by TheDS1337; 01-02-2016 at 13:36.
TheDS1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 01-02-2016 , 18:01   Re: C++ Questions
Reply With Quote #18

Here, I wrote some code to show you how you could load a string of indeterminate length. It's more C-like, and as I said, if I were to write real C++ code, I would use std::string and such.
This pretty much loads characters one-by-one, and if the buffer is close to overflowing, I increase its size. Sample output:


And here, even though you will see in following code that an array starts with size of 16, it can load much larger strings with no problem:


Code:
PHP Code:
#include <iostream>


char *firstToUpperchar(char *);

class 
CFriend {
private:
    
char *pName;
    
char *pNickname;

    
// Default constructor is private, so the user has to use one below
    
CFriend() {

    }

public:
    
CFriend(char *namechar *nickname) : pName(firstToUpperchar(name)), pNickname(firstToUpperchar(nickname)) {
    }

    ~
CFriend() {
        
// Free memory allocated by readString
        
free(pName);
        
free(pNickname);
    }

    
// These 2 methods are used so user can't change name and nickname
    
char * const getName() const {
        return 
pName;
    }
    
char * const getNickname() const {
        return 
pNickname;
    }
};

char *firstToUpperchar(char *str) {
    if (*
str >= 'a' && *str <= 'z') {
        *
str += 'A' 'a';
    }

    return 
str;
}

// Caller is responsible for freeing memory allocated by this function
char *readString() {
    static const 
size_t sizeStep 16// We add 16 bytes to the allocated block each time it would overflow
    
size_t memSize 16// We start with 16 chars
    
size_t len 0// Length of the string we load in
    
char *buf = (char *)malloc(sizeof(char) * memSize); // Allocate first memSize bytes
    
char ch;

    for (;;) { 
// Infinite loop
        
ch getchar();
        if (
ch == EOF || ch == '\n') { // We have reached the end, break out
            
break;
        }

        
buf[len++] = ch;
        
// Buffer would overflow if we added one more char
        
if (len == memSize) { 
            
memSize += sizeStep// Increase memory block size by sizeStep chars
            
buf = (char *)realloc(bufsizeof(char) * memSize); // And reallocate the array
        
}
    }
    
buf[len] = '\0';

    return 
buf;
}

int main(int argcchar *argv[]) {
    
int friendsCount;
    
std::cout << "How many friends do you have? ";
    
std::cin >> friendsCount;
    
std::cin.get(); // This just removes the trailing '\n' from the stream
    
int i;

    
// We are actually creating an array of POINTERS to CFriend, we are not initializing these objects, we'll do it later
    
CFriend **pFriends = new CFriend*[friendsCount];

    
char *name, *nickname;
    for (
0friendsCounti++) {
        
std::cout << "Name of friend #" << (1) << ": ";
        
name readString();
        
std::cout << "Nickname of friend #" << (1) << ": ";
        
nickname readString();

        
// Here is where we actually create an object
        
pFriends[i] = new CFriend(namenickname);
    }

    
std::cout << "Your friends are:" << std::endl;
    
std::cout << "-----------------" << std::endl;

    for (
0friendsCounti++) {
        
std::cout << (1) << ". " << pFriends[i]->getName() << " (" << pFriends[i]->getNickname() << ")" << std::endl;
    }

    
std::cout << "-----------------" << std::endl;

    
getchar(); // This works like system("PAUSE"), but should be more platform-independant
    
delete[] pFriends;
    return 
0;

klippy is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-03-2016 , 01:16   Re: C++ Questions
Reply With Quote #19

Oh, really nice! this is exactly what I wanted a dynamic buffer. thank you for your work m8!
TheDS1337 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:07.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode