0

I created structure Route.h

#include "stdafx.h"
using namespace std;

struct Route {
    string startPoint;
    string endPoint;
    int number;
};

and I need to pass this structure to function. I used reference:

void CreateRoute(Route &route)
{
  int start = rand() % 10;
  int end = rand() % 10;

  if (start == end)
  {
    while(true)
    {
      end = rand()%10;
      if(end != start) break;
    }
  }

  route.startPoint = SetPoint(start);
  route.endPoint = SetPoint(end);
  route.number = SetNumber();
}

but it seems the using of pointers is the better way to do it, but I don't know how to use pointers for it?

6
  • 1
    Why do you think you should use pointers? I don't see any reason for that. Oct 16, 2013 at 10:00
  • 2
    "Better" is clearly subjective, but you might also consider making Route the return type.
    – NPE
    Oct 16, 2013 at 10:00
  • 1
    You might want to have a look at stackoverflow.com/questions/57483/… Oct 16, 2013 at 10:01
  • @juanchopanza Because I have read the using of poiners is more powerful way to do it.
    – Heidel
    Oct 16, 2013 at 10:02
  • 2
    In this case, I would just have the function make a new object and retrurn it. Copy can be optimized by move. Oct 16, 2013 at 10:09

3 Answers 3

3

In this case, why aren't you simply returning a newly constructed object?

struct route
{
    std::string start_point;
    std::string end_point;
    int number;
};

route make_random_route()
{
    route r;

    int start = std::rand() % 10;
    int end = std::rand() % 10;

    while ( start == end) {
        end = std::rand() % 10;
    }

    r.start_point = make_point(start);
    r.end_point = make_point(end);
    r.number = make_number();

    return r;
}

Its trivial, and with move there is no copy.

1
  • Yes, by now I changed my all function and made Route the return type, Route CreateRoute().
    – Heidel
    Oct 16, 2013 at 10:20
2

but it seems the using of pointers is the better way to do it

One of the reasons C++ has references to begin with is to get around the hassle of dealing with pointers, arrows and lots of parentheses.

You could easily convert it to use a pointer type, but the ref type is just cleaner.

void CreateRoute(Route* route);

would be your declaration, and you would call it using

Route route;
CreateRoute(&route);
4
  • 1
    You don't need to call new at all. That will just add to the confusion. Oct 16, 2013 at 10:05
  • It doesnt work if I call it using CreateRoute(&route[i]); (I need to pass member of array) but it works if I call it using CreateRoute(route[i]);
    – Heidel
    Oct 16, 2013 at 10:10
  • You changed the function definition to use arrows instead of dots, right?
    – sandymatt
    Oct 16, 2013 at 10:12
  • By now I changed my all function and made Route the return type, Route CreateRoute().
    – Heidel
    Oct 16, 2013 at 10:15
1

I think you must improve your C++ basis. Below is my simple answer.

void CreateRoute(Route *route)
{
if (route == NULL)
    return;

int start = rand()%10;
int end = rand()%10;

if (start == end)
{
    while(true)
    {
        end = rand()%10;
        if(end != start) break;
    }
}

route->startPoint = SetPoint(start);
route->endPoint = SetPoint(end);
route->number = SetNumber();
}
1
  • 1
    While correct, this does nothing to dispel OP's strange notions about passing a pointer in the first place. Oct 16, 2013 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.