0

In the for loop below:

struct Block
{
    Block(int d) : data(d), next(nullptr) {}

    int data;
    Block* next;
};

Block* aList = new Block(1);
Block* bList = new Block(2);

for (Block* a = aList, *b = bList; a != nullptr; a = a->next, b = b->next)
    if (aList->data != bList->data)
        cout << "Difference found.\n";

I don't like putting the * before b, but of course it's needed to distinguish Block from Block*. Is there another way to do this? for ((Block*) a, b... isn't a go.

Topological Sort
  • 2,733
  • 2
  • 27
  • 54
  • Not exactly the same question, but still really close to be a duplicate [Declaring multiple object pointers on one line causes compiler error](https://stackoverflow.com/questions/13618282/declaring-multiple-object-pointers-on-one-line-causes-compiler-error) – t.niese May 09 '19 at 14:12
  • Yes, that's a good point, and I have edited the question. – Topological Sort May 09 '19 at 14:20
  • @TopologicalSort when you edit the question and change it, please do it in a separate section (or open a new one if it totally another - witch is not the case here), so answers that were relevant to the original question, will stay so... – Coral Kashri May 09 '19 at 14:37
  • @KorelK No, don't do that. We don't want a timeline of changes. We can see old revisions in the post's history. _Integrate updates into the flow of the post,_ as the OP has done here. – Lightness Races in Orbit May 09 '19 at 14:55

6 Answers6

5

You might do it like this:

for (auto a = aList, b = bList; a != nullptr; a = a->next, b = b->next)
    if (aList->data != bList->data)
        cout << "Difference found.\n";
vahancho
  • 20,808
  • 3
  • 47
  • 55
2

If you don't want to repeat the * then you could use using and create an alias BlockPtr which you use instead of Block*:

int main() {
  using BlockPtr = Block*;

  BlockPtr aList = new Block(1);
  BlockPtr bList = new Block(2);

  for (BlockPtr a = aList, b = bList; a != nullptr; a = a->next, b = b->next)
    if (aList->data != bList->data)
      cout << "Difference found.\n";
}

Or relay on auto:

int main() {

  auto aList = new Block(1);
  auto bList = new Block(2);

  for (auto a = aList, b = bList; a != nullptr; a = a->next, b = b->next)
    if (aList->data != bList->data)
      cout << "Difference found.\n";
}
t.niese
  • 39,256
  • 9
  • 74
  • 101
1

Yes, just use:

Block* a = aList, *b = bList

EDIT:

Option 1 - Using Boost

#include <boost/typeof/typeof.hpp>
/*
    ...
*/
for (BOOST_TYPEOF_KEYWORD(Block*) a = aList, b = bList;...)

Another option is to create a single variable of the type you want, and use it's type to initialize other variables (similar to auto):

Option 2

Block* aList = new Block(1);
Block* bList = new Block(2);

for (decltype(aList) a = aList, b = bList; ...) ...

Option 3 - Using Boost

#include <boost/typeof/typeof.hpp>
/*
    Like the first option
*/
for (BOOST_TYPEOF(aList) a = aList, b = bList;...) ...
// ...
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22
1

When declaring pointers, the * belongs to the name, not the type. That means you can make b a pointer like

for (Block *a = aList, *b = bList; a != nullptr; a = a->next, b = b->next)
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
1

You are trying to declare two pointers in an expression.

Block* a = aList, b = bList;

It happens to be part of a for loop, but just as

int * a, * b;

is two int pointers, you can use

Block* a = aList, * b = bList;

in your for loop.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

What about just defining a type alias?

using BlockPtr = Block*;

for (BlockPtr a = aList, b = bList; a != nullptr; a = a->next, b = b->next)
    if (aList->data != bList->data)
        cout << "Difference found.\n";
andreee
  • 4,459
  • 22
  • 42