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.

4
  • Not exactly the same question, but still really close to be a duplicate Declaring multiple object pointers on one line causes compiler error Commented May 9, 2019 at 14:12
  • Yes, that's a good point, and I have edited the question. Commented May 9, 2019 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... Commented May 9, 2019 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. Commented May 9, 2019 at 14:55

6 Answers 6

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";
Sign up to request clarification or add additional context in comments.

2 Comments

This is probably the best option as long as you want them all to be the same type. +1.
I'm not a big fan of the type hiding tbh, but aList and bList are lexically near so I guess it's okay
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";
}

Comments

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)

Comments

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.

Comments

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;...) ...
// ...

Comments

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";

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.