More efficient way of coding this?

Hello everyone! New here and starting to learn programming recently. I decided to start with C++ despite the difficulty / flexibility. Anyway, I went through every tutorial on this site, writing practice programs along with it.

Now I'm reading a book called Learning C++ Through Game Programming. Unfortunately it doesn't go by C++11 like your site does, so I'm wondering if there is a more efficient way of coding this regardless.

There's an exercise that says to write a program that uses pointers and gets the size of a string. My code is below. It runs and compiles without any error but I'm not exactly sure if I'm even using pointers correctly either. If there's any other or more efficient way of coding it, please let me know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;


string str;
string* pString = &str;
void getStrSize(string* sName); // function for getting string size

int main()
{
	cout << "Enter a string to get it's size: ";
	getline(cin, str);
	getStrSize(pString);
	
	cin.get(); // requires entering of a key to close program
	return 0;
}

void getStrSize(string* sName)
{
	*pString = str;
	cout << pString->size();
}
I have experienced C++ for more than 5 years and I am still learning, too. I will show you how I do for this program. hoping it will help you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;


// this is passby reference, it looks better than passby pointer. use const as much as you can
void getStrSize(const string & sName); // function for getting string size

int main()
{
    std::string str;
    cout << "Enter a string to get it's size: ";
    getline(cin, str);
    getStrSize(str);
    cin.get(); // requires entering of a key to close program
    return 0;
}

void getStrSize(const string & sName)
{
    cout << sName.size();
}
Last edited on
I would advise against using global variables. They make your program hard to maintain when it gets bigger. Instead pass variables as parameters.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

size_t GetStrSize( const std::string* const str );

int main( )
{
	std::string str{ "Hello World" };
	std::cout << GetStrSize( &str ) << "\n";
}

inline size_t GetStrSize( const std::string* const str )
{
	return str->size( );
}


11
Last edited on
Thanks to the both of you for your tips. I didn't realize you could declare variables within functions. Very handy to know.

As for littlepig, that's probably how I would have programmed it normally, but the exercise in the book specifically said I had to use pointers. But nonetheless, thank you for posting that!
1
2
3
4
5
std::size_t str_size( const std::string* p )
{
    if( p == nullptr ) return 0 ;
    else return p->size() ;
}
closed account (D80DSL3A)
@JLBorges. Why your practice of following a conditional return with else?
1
2
if( cond ) return;
// all that follows is the "else" 

Is it just to reduce confusion for the reader?
Or is there actual need for it?

Do you follow this practice in your own code?
Because if there wasn't a return in the if statement, the code after it would be executed. I think it's just a force of habit.
> Is it just to reduce confusion for the reader?

Yes.


> is there actual need for it?

No.


> Do you follow this practice in your own code?

Only if there is a single return statement after the if.

In my own code, I would have very few functions that accept a pointer (or a smart pointer) to an object.
Prefer T* over T& when "no argument" is a valid option

Reason

A pointer (T*) can be a nullptr and a reference (T&) cannot, there is no valid "null reference". Sometimes having nullptr as an alternative to indicated "no object" is useful, but if it is not, a reference is notationally simpler and might yield better code.
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rf-ptr-ref
Topic archived. No new replies allowed.