2

in the method bool Foam::pimpleControl::criteriaSatisfied() of an OpenFoam Source Code I found the following expression:

bool Foam::pimpleControl::criteriaSatisfied()
{
    // ...
    const word& variableName = iter().keyword();
    // ...
}

For iter() I found: Foam::label iter() const inline Return const access to the current cloud iteration

For keyword():

keyType& keyword() inline Return non-const access to keyword.

I have two questions about this:

  1. What does it mean when calling method on method like iter().keyword();?

  2. What does & after word& or keyType& mean? I know that every method also has a datatyp declaration, but has the & a specific meaning?

greetings Streight

1
  • 1
    "Foam::label iter() const inline Return const access to the current cloud iteration" - Come again? Commented Oct 28, 2013 at 18:55

2 Answers 2

4

What does it mean when calling method on method like iter().keyword();

iter() returns an object. keyword() then calls a method of that object. It's basically just a shorthand for Foam::label temp = iter(); temp.keyword().

What does & after word& or keyType& mean?

The ampersand is part of the type. const word& names the type "reference to const word".

Sign up to request clarification or add additional context in comments.

Comments

1

You're not calling a "method on method". "iter()" is returning an object and that object has a member "keyword()".

The code is equivalent to

Foam::label& it = iter();
it.keyword();

here's a simplified example:

class A {
    int m_i;
public:
    A(int i) : m_i(i) {}
    int GetI() const { return m_i; }
};

class B {
    A m_a;
public:
    B(int i) : m_a(i) {}
    const A& GetA() const { return m_a; }
};

#include <iostream>

int main() {
    B b(42);

    const A& a = b.GetA();
    std::cout << "a.GetI() == " << a.GetI() <<std::endl;

    // above code is directly equivalent to:
    std::cout << "b.GetA().GetI() == " << b.GetA().GetI() <<std::endl;
}

In the last line, "b.GetA()" returns a temporary, anonymous "const A&" object, and then we call "GetI()" on that object.

1 Comment

Thx for the example, I just gave the other answer the solved flag, because it appeared first and also answered my question.

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.